package com.hxgis.common.service.impl;

import com.es.common.EsAggCondition;
import com.es.common.domain.EsCondition;
import com.es.common.domain.RFFM;
import com.es.common.domain.agg.Group;
import com.es.common.enums.FlagConnect;
import com.es.common.enums.Order;
import com.es.wrapper.DataWrapper;
import com.hxgis.common.base.ResultVO;
import com.hxgis.common.base.StationCache;
import com.hxgis.common.dto.*;
import com.hxgis.common.entity.RainDayInfo;
import com.hxgis.common.entity.Station;
import com.hxgis.common.enums.DayRangeEnum;
import com.hxgis.common.enums.PreYear30Enum;
import com.hxgis.common.enums.RainFlagEnum;
import com.hxgis.common.parms.RangeParm;
import com.hxgis.common.repository.RainDayInfoRepository;
import com.hxgis.common.service.RainAnalysisService;
import com.hxgis.common.service.RainHourService;
import com.hxgis.common.service.graph.RainStormGraph;
import com.hxgis.common.utils.*;
import com.hxgis.common.vo.RainDayRangeVO;
import com.hxgis.common.vo.RainInfoAnalysisVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author cqj
 * @version 1.0
 * @date 2019/10/29 10:08
 */
@Service
@Slf4j
public class RainAnalysisServiceImpl implements RainAnalysisService {

    @Value("${save.colorFigPath}")
    private String colorFigPath;

    @Autowired
    private RainStormGraph rainStormGraph;

    @Autowired
    RainDayInfoRepository dayInfoRepository;

    @Autowired
    private RainHourService rainHourService;

    /**
     * 查询极端事件的单站各种指标的阈值信息
     * @param startTime 开始时间 19810101
     * @param endTime 结束时间 20001231
     * @return
     */
//    @Override
//    public List<ExtremeEventDTO> getThresholdAnalysis(String startTime, String endTime) {
//
//        //站点基础信息
//        List<Station> stations = StationCache.getStations();
//        List<ExtremeEventDTO> extremeEventDTOS = new ArrayList<>();
//
//        Map<String, Double> extremeEventRain1Map = getExtremeEventRainDTOS(startTime, endTime, "20", 1, 3);
//        Map<String, Double> extremeEventRain2Map = getExtremeEventRainDTOS(startTime, endTime, "20", 2, 3);
//        Map<String, Double> extremeEventRain3Map = getExtremeEventRainDTOS(startTime, endTime, "20", 3, 3);
//        Map<String, Double> extremeEventRain4Map = getExtremeEventRainDTOS(startTime, endTime, "20", 4, 3);
//        Map<String, Double> extremeEventRain5Map = getExtremeEventRainDTOS(startTime, endTime, "20", 5, 3);
//        Map<String, Double> extremeEventRain6Map = getExtremeEventRainDTOS(startTime, endTime, "20", 6, 3);
//        Map<String, Double> extremeEventRain7Map = getExtremeEventRainDTOS(startTime, endTime, "20", 7, 3);
//        Map<String, Double> extremeEventRain8Map = getExtremeEventRainDTOS(startTime, endTime, "20", 8, 3);
//        Map<String, Double> extremeEventRain9Map = getExtremeEventRainDTOS(startTime, endTime, "20", 9, 3);
//        Map<String, Double> extremeEventRain10Map = getExtremeEventRainDTOS(startTime, endTime, "20", 10, 3);
//        Map<String, Double> extremeEventRain11Map = getExtremeEventRainDTOS(startTime, endTime, "20", 11, 3);
//        Map<String, Double> extremeEventRain12Map = getExtremeEventRainDTOS(startTime, endTime, "20", 12, 3);
//        Map<String, Double> extremeEventRain13Map = getExtremeEventRainDTOS(startTime, endTime, "20", 13, 3);
//        Map<String, Double> extremeEventRain14Map = getExtremeEventRainDTOS(startTime, endTime, "20", 14, 3);
//        Map<String, Double> extremeEventRain15Map = getExtremeEventRainDTOS(startTime, endTime, "20", 15, 3);
//
//        Map<String, Double> extreme08EventRain1Map = getExtremeEventRainDTOS(startTime, endTime, "08", 1, 3);
//        Map<String, Double> extreme08EventRain2Map = getExtremeEventRainDTOS(startTime, endTime, "08", 2, 3);
//        Map<String, Double> extreme08EventRain3Map = getExtremeEventRainDTOS(startTime, endTime, "08", 3, 3);
//        Map<String, Double> extreme08EventRain4Map = getExtremeEventRainDTOS(startTime, endTime, "08", 4, 3);
//        Map<String, Double> extreme08EventRain5Map = getExtremeEventRainDTOS(startTime, endTime, "08", 5, 3);
//        Map<String, Double> extreme08EventRain6Map = getExtremeEventRainDTOS(startTime, endTime, "08", 6, 3);
//        Map<String, Double> extreme08EventRain7Map = getExtremeEventRainDTOS(startTime, endTime, "08", 7, 3);
//        Map<String, Double> extreme08EventRain8Map = getExtremeEventRainDTOS(startTime, endTime, "08", 8, 3);
//        Map<String, Double> extreme08EventRain9Map = getExtremeEventRainDTOS(startTime, endTime, "08", 9, 3);
//        Map<String, Double> extreme08EventRain10Map = getExtremeEventRainDTOS(startTime, endTime, "08", 10, 3);
//        Map<String, Double> extreme08EventRain11Map = getExtremeEventRainDTOS(startTime, endTime, "08", 11, 3);
//        Map<String, Double> extreme08EventRain12Map = getExtremeEventRainDTOS(startTime, endTime, "08", 12, 3);
//        Map<String, Double> extreme08EventRain13Map = getExtremeEventRainDTOS(startTime, endTime, "08", 13, 3);
//        Map<String, Double> extreme08EventRain14Map = getExtremeEventRainDTOS(startTime, endTime, "08", 14, 3);
//        Map<String, Double> extreme08EventRain15Map = getExtremeEventRainDTOS(startTime, endTime, "08", 15, 3);
//
//
//        //循环全站点信息
//        stations.stream().forEach(x -> {
//            ExtremeEventDTO extremeEventDTO = new ExtremeEventDTO();
//            extremeEventDTO.setStationNo(x.getStationNo());
//            extremeEventDTO.setRainThreshold20_1(Objects.isNull(extremeEventRain1Map.get(x.getStationNo())) ? 0d : extremeEventRain1Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_2(Objects.isNull(extremeEventRain2Map.get(x.getStationNo())) ? 0d : extremeEventRain2Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_3(Objects.isNull(extremeEventRain3Map.get(x.getStationNo())) ? 0d : extremeEventRain3Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_4(Objects.isNull(extremeEventRain4Map.get(x.getStationNo())) ? 0d : extremeEventRain4Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_5(Objects.isNull(extremeEventRain5Map.get(x.getStationNo())) ? 0d : extremeEventRain5Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_6(Objects.isNull(extremeEventRain6Map.get(x.getStationNo())) ? 0d : extremeEventRain6Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_7(Objects.isNull(extremeEventRain7Map.get(x.getStationNo())) ? 0d : extremeEventRain7Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_8(Objects.isNull(extremeEventRain8Map.get(x.getStationNo())) ? 0d : extremeEventRain8Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_9(Objects.isNull(extremeEventRain9Map.get(x.getStationNo())) ? 0d : extremeEventRain9Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_10(Objects.isNull(extremeEventRain10Map.get(x.getStationNo())) ? 0d : extremeEventRain10Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_11(Objects.isNull(extremeEventRain11Map.get(x.getStationNo())) ? 0d : extremeEventRain11Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_12(Objects.isNull(extremeEventRain12Map.get(x.getStationNo())) ? 0d : extremeEventRain12Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_13(Objects.isNull(extremeEventRain13Map.get(x.getStationNo())) ? 0d : extremeEventRain13Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_14(Objects.isNull(extremeEventRain14Map.get(x.getStationNo())) ? 0d : extremeEventRain14Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold20_15(Objects.isNull(extremeEventRain15Map.get(x.getStationNo())) ? 0d : extremeEventRain15Map.get(x.getStationNo()));
//
//            extremeEventDTO.setRainThreshold08_1(Objects.isNull(extreme08EventRain1Map.get(x.getStationNo())) ? 0d : extreme08EventRain1Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_2(Objects.isNull(extreme08EventRain2Map.get(x.getStationNo())) ? 0d : extreme08EventRain2Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_3(Objects.isNull(extreme08EventRain3Map.get(x.getStationNo())) ? 0d : extreme08EventRain3Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_4(Objects.isNull(extreme08EventRain4Map.get(x.getStationNo())) ? 0d : extreme08EventRain4Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_5(Objects.isNull(extreme08EventRain5Map.get(x.getStationNo())) ? 0d : extreme08EventRain5Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_6(Objects.isNull(extreme08EventRain6Map.get(x.getStationNo())) ? 0d : extreme08EventRain6Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_7(Objects.isNull(extreme08EventRain7Map.get(x.getStationNo())) ? 0d : extreme08EventRain7Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_8(Objects.isNull(extreme08EventRain8Map.get(x.getStationNo())) ? 0d : extreme08EventRain8Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_9(Objects.isNull(extreme08EventRain9Map.get(x.getStationNo())) ? 0d : extreme08EventRain9Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_10(Objects.isNull(extreme08EventRain10Map.get(x.getStationNo())) ? 0d : extreme08EventRain10Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_11(Objects.isNull(extreme08EventRain11Map.get(x.getStationNo())) ? 0d : extreme08EventRain11Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_12(Objects.isNull(extreme08EventRain12Map.get(x.getStationNo())) ? 0d : extreme08EventRain12Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_13(Objects.isNull(extreme08EventRain13Map.get(x.getStationNo())) ? 0d : extreme08EventRain13Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_14(Objects.isNull(extreme08EventRain14Map.get(x.getStationNo())) ? 0d : extreme08EventRain14Map.get(x.getStationNo()));
//            extremeEventDTO.setRainThreshold08_15(Objects.isNull(extreme08EventRain15Map.get(x.getStationNo())) ? 0d : extreme08EventRain15Map.get(x.getStationNo()));
//            extremeEventDTOS.add(extremeEventDTO);
//
//        });
//        return extremeEventDTOS;
//    }
//
//    private Map<String, Double> getExtremeEventRainDTOS(String startTime, String endTime, String flag, Integer day, Integer index) {
//        //根据站点分组
//        EsAggCondition esAggCondition = EsAggCondition.getInstance();
//        Group stationNoGroup = esAggCondition.Group("stationNo");
//        stationNoGroup.size(10000);
//        Group yearGroup = stationNoGroup.groupSub("year");
//        yearGroup.size(30);
//        Group timeGroup = yearGroup.groupSub("time");
//        if (Objects.equals(day, 1)){
//            timeGroup.sum(RainFlagEnum.getRainFlagEnum(flag).getMessage(),"rainFallSum");
//        }else{
//            timeGroup.sum(RainFlagEnum.getRainFlagEnum(flag).getMessage()+"_"+day,"rainFallSum");
//        }
//        timeGroup.order(Order.DESC,"rainFallSum");
//        timeGroup.size(2);
//        EsCondition condition = esAggCondition.getCondition();
//        condition.gte("time",startTime).lte(endTime)
//                .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
//        List<RainInfoAnalysDTO> rainInfoAnalysDTOS = dayInfoRepository.selectListAgg(esAggCondition, new DataWrapper<RainInfoAnalysDTO>() {
//            @Override
//            public RainInfoAnalysDTO wrapper(Map<String, Object> data) {
//                RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
//                rainInfoAnalysDTO.setStationNo(data.get("stationNo").toString());
//                rainInfoAnalysDTO.setYear(data.get("year").toString());
//                rainInfoAnalysDTO.setTime(data.get("time").toString());
//                rainInfoAnalysDTO.setRainFallSum(Double.parseDouble(data.get("rainFallSum").toString())/10);
//                return rainInfoAnalysDTO;
//            }
//        });
//
//        Map<String,List<RainInfoAnalysDTO>> stationExtremeMap = rainInfoAnalysDTOS.stream().collect(Collectors.groupingBy(RainInfoAnalysDTO::getStationNo));
//        //站点id -》 对应的值
//        Map<String, Double> map = new HashMap<>();
//        stationExtremeMap.forEach((stationNo,values) -> {
//            //按照时间排序返回前端
//            Collections.sort(values,((o1, o2) -> {
//                return (int)(o2.getRainFallSum() * 10 -o1.getRainFallSum() * 10);
//            }));
//            map.put(stationNo, values.get(index - 1).getRainFallSum());
//        });
//        return map;
//    }

    @Override
    public ResultVO intelligenceByRainDay(String startTime, String endTime, String leftTime, String rightTime, String type, String flag20, String flag08, String stationNum) {
        Long st = System.currentTimeMillis();

        //todo 站点基础信息 ==》 可以根据省份查询站点信息 =》 减少查询量 =》 加快查询速度
        List<Station> stations = StationCache.getStations();

        List<RainInfoAnalysDTO> rainInfoAnalysDTOS = new ArrayList<>();
        //todo 从es查询数据 注意跨年的情况
        Integer rightYear = null;
        if (Objects.equals(startTime.substring(0,4),endTime.substring(0,4))){
            //无跨年数据直接处理返回
            rightYear = Integer.parseInt(rightTime.substring(0,4));
            rainInfoAnalysDTOS = getRainInfoAnalysDTOS(startTime, endTime, leftTime, rightTime, type, flag20, flag08, stationNum);
        }else{
            //处理跨年数据 需要分两段查询 最后进行拼接
            rightYear = Integer.parseInt(startTime.substring(0,4));
            List<RainInfoAnalysDTO> leftRainInfoAnalysDTOS = getRainInfoAnalysDTOS(startTime, rightYear + "1231", leftTime, rightYear + "1231", type, flag20, flag08, stationNum);
            List<RainInfoAnalysDTO> rightRainInfoAnalysDTOS = getRainInfoAnalysDTOS(endTime.substring(0,4) + "0101", endTime, leftTime, rightTime, type, flag20, flag08, stationNum);

            //组装跨年的数据
            rainInfoAnalysDTOS = handleRainInfoAnalysDTO(leftRainInfoAnalysDTOS, rightRainInfoAnalysDTOS, leftTime.substring(0,4), endTime.substring(0,4));

        }


        log.info("情报分析查询降雨日查询es用时{}",System.currentTimeMillis() - st);
        Map<String, Map<String, List<RainInfoAnalysDTO>>> collect = rainInfoAnalysDTOS.stream().collect(Collectors.groupingBy(RainInfoAnalysDTO::getStationNo, Collectors.groupingBy(RainInfoAnalysDTO::getYear)));
        List<RainInfoAnaStationDTO> singStationInfos = new ArrayList<>();

        //单站的信息
        if (Objects.nonNull(stationNum) && !Objects.equals(stationNum,"")){
            Map<String, List<RainInfoAnalysDTO>> singleRainInfoMapByYear = collect.get(stationNum);
            List<YearDataValuesDTO> yearDataValuesDTOS = new ArrayList<>();
            if (CollectionUtils.isEmpty(singleRainInfoMapByYear)){
                for (int i = Integer.parseInt(leftTime.substring(0, 4)); i <= rightYear; i++) {
                    YearDataValuesDTO yearDataValuesDTO = new YearDataValuesDTO();
                    yearDataValuesDTO.setYear(String.valueOf(i));
                    yearDataValuesDTO.setAvg30(0d);
                    yearDataValuesDTO.setDataValues(0d);
                    yearDataValuesDTO.setAnomalyPercent(0d);
                    yearDataValuesDTO.setHistoryRange(0);
                    yearDataValuesDTOS.add(yearDataValuesDTO);
                }
                return ResultVOUtil.success(yearDataValuesDTOS);
            }
            //不为空
            for (int i = Integer.parseInt(leftTime.substring(0, 4)); i <= rightYear; i++) {
                YearDataValuesDTO yearDataValuesDTO = new YearDataValuesDTO();
                yearDataValuesDTO.setYear(String.valueOf(i));
                //单站30年
                DoubleSummaryStatistics singleRainInfoAvg30Sum = rainInfoAnalysDTOS.stream().filter((p) -> (Integer.parseInt(p.getYear()) >= PreYear30Enum.PRE_YEAR_30_START.getYear() &&
                        Integer.parseInt(p.getYear()) <= PreYear30Enum.PRE_YEAR_30_END.getYear()))
                        .collect(Collectors.summarizingDouble(RainInfoAnalysDTO::getCountDays));
                double avg30PreStation = singleRainInfoAvg30Sum.getSum() / 30;

                //当前年实况值
                List<RainInfoAnalysDTO> rainInfoAnalysDTOList = singleRainInfoMapByYear.get(String.valueOf(i));
                Double nowYearRainSum;
                if (CollectionUtils.isEmpty(rainInfoAnalysDTOList)){
                    nowYearRainSum = 0d;
                }else{
//                    DoubleSummaryStatistics collect1 = rainInfoAnalysDTOList.stream().collect(Collectors.summarizingDouble(RainInfoAnalysDTO::getRainYearSum));
                    nowYearRainSum = rainInfoAnalysDTOList.get(0).getCountDays();
                }

                BigDecimal b1 = new BigDecimal(Double.toString(avg30PreStation));
                BigDecimal b2 = new BigDecimal(Double.toString(nowYearRainSum));
                BigDecimal subtract = b2.subtract(b1);
//                double v = subtract.divide(b1, 1, BigDecimal.ROUND_HALF_UP).doubleValue();

                yearDataValuesDTO.setDataValues(NumUtil.save1Point(nowYearRainSum));
                yearDataValuesDTO.setAnomalyPercent(NumUtil.save1Point(subtract.doubleValue()));
                yearDataValuesDTO.setAvg30(NumUtil.save1Point(avg30PreStation));
                yearDataValuesDTOS.add(yearDataValuesDTO);
            }
            //按照实况值排序
            Collections.sort(yearDataValuesDTOS,((o1, o2) -> {
                return (int)(o2.getDataValues()*100 - o1.getDataValues()*100);
            }));
            //排名信息
            for (int i = 0; i < yearDataValuesDTOS.size(); i++) {
                YearDataValuesDTO yearDataValuesDTO = yearDataValuesDTOS.get(i);
                yearDataValuesDTO.setHistoryRange(i + 1);
            }

            //按照时间排序返回前端
            Collections.sort(yearDataValuesDTOS,((o1, o2) -> {
                return Integer.parseInt(o1.getYear()) - Integer.parseInt(o2.getYear());
            }));
            return ResultVOUtil.success(yearDataValuesDTOS);
        }




        stations.stream().forEach(x -> {
            RainInfoAnaStationDTO rainInfoAnaStationDTO = new RainInfoAnaStationDTO();
            rainInfoAnaStationDTO.setLat(Double.valueOf(x.getLat()));
            rainInfoAnaStationDTO.setLon(Double.valueOf(x.getLon()));
            rainInfoAnaStationDTO.setStationNo(x.getStationNo());
            rainInfoAnaStationDTO.setStationName(x.getCity());
            rainInfoAnaStationDTO.setProvince(x.getProvince());
            rainInfoAnaStationDTO.setObserDate(startTime.substring(0,4));
            double nowDays =  CollectionUtils.isEmpty(collect.get(x.getStationNo())) ? 999999d :
                    CollectionUtils.isEmpty(collect.get(x.getStationNo()).get(startTime.substring(0, 4))) ? 0d
                            : collect.get(x.getStationNo()).get(startTime.substring(0, 4)).get(0).getCountDays();
            rainInfoAnaStationDTO.setValues(nowDays);
            //30年均值
            Map<String, List<RainInfoAnalysDTO>> stringListMap = collect.get(x.getStationNo());
            if (CollectionUtils.isEmpty(stringListMap)){
                rainInfoAnaStationDTO.setAvg30(999999d);
                rainInfoAnaStationDTO.setMaxValues(999999d);
                rainInfoAnaStationDTO.setMinValues(999999d);
                rainInfoAnaStationDTO.setMaxHistoryAvgRate(999999d);
                rainInfoAnaStationDTO.setMinHistoryAvgRate(999999d);
                rainInfoAnaStationDTO.setHistoryAvgRate(999999d);
                rainInfoAnaStationDTO.setRangeNum(999999);
            }else{
                //再去筛选1980-2010的数据
                List<RainInfoAnalysDTO> rainInfoAnalysDTOList = new ArrayList<>();
                stringListMap.forEach((year,value1) -> {
                    rainInfoAnalysDTOList.addAll(value1);
                });
                List<RainInfoAnalysDTO> collect1 = rainInfoAnalysDTOList.stream().filter((p) -> (Integer.parseInt(p.getYear()) >= PreYear30Enum.PRE_YEAR_30_START.getYear() &&
                        Integer.parseInt(p.getYear()) <= PreYear30Enum.PRE_YEAR_30_END.getYear()))
                        .collect(Collectors.toList());
                if (CollectionUtils.isEmpty(collect1)){
                    rainInfoAnaStationDTO.setAvg30(999999d);
                    rainInfoAnaStationDTO.setMaxHistoryAvgRate(999999d);
                    rainInfoAnaStationDTO.setMinHistoryAvgRate(999999d);
                    rainInfoAnaStationDTO.setHistoryAvgRate(999999d);
                    rainInfoAnaStationDTO.setRangeNum(999999);
                    //查询的所有数据按照持续日数排序求最大最小
                    Collections.sort(rainInfoAnalysDTOList,((o1, o2) -> {
                        return (int)(o1.getCountDays()*100 - o2.getCountDays()*100);
                    }));

                    Double minDays = null;
                    //todo 最小值要特殊处理下，因为最小值可能为0
                    for (int i = Integer.parseInt(leftTime.substring(0, 4)); i <= Integer.parseInt(rightTime.substring(0, 4)); i++) {
                        List<RainInfoAnalysDTO> rainInfoAnalysDTOList1 = stringListMap.get(String.valueOf(i));
                        if (CollectionUtils.isEmpty(rainInfoAnalysDTOList1)){
                            minDays = 0d;
                        }
                    }
                    if (!Objects.equals(minDays,0d)){
                        minDays = rainInfoAnalysDTOList.get(0).getCountDays();
                    }

                    Double maxDays = rainInfoAnalysDTOList.get(rainInfoAnalysDTOList.size() - 1).getCountDays();
                    rainInfoAnaStationDTO.setMinValues(minDays);
                    rainInfoAnaStationDTO.setMaxValues(maxDays);

                }else{
                    DoubleSummaryStatistics collect2 = collect1.stream().collect(Collectors.summarizingDouble(RainInfoAnalysDTO::getCountDays));
                    double v = collect2.getSum() / 30;
                    rainInfoAnaStationDTO.setAvg30(NumUtil.save1Point(v));
                    //查询的所有数据按照持续日数排序求最大最小
                    Collections.sort(rainInfoAnalysDTOList,((o1, o2) -> {
                        return (int)(o1.getCountDays()*100 - o2.getCountDays()*100);
                    }));

                    Double minDays = null;
                    //todo 最小值要特殊处理下，因为最小值可能为0
                    for (int i = Integer.parseInt(leftTime.substring(0, 4)); i <= Integer.parseInt(rightTime.substring(0, 4)); i++) {
                        List<RainInfoAnalysDTO> rainInfoAnalysDTOList1 = stringListMap.get(String.valueOf(i));
                        if (CollectionUtils.isEmpty(rainInfoAnalysDTOList1)){
                            minDays = 0d;
                        }
                    }
                    if (!Objects.equals(minDays,0d)){
                        minDays = rainInfoAnalysDTOList.get(0).getCountDays();
                    }

                    Double maxDays = rainInfoAnalysDTOList.get(rainInfoAnalysDTOList.size() - 1).getCountDays();
                    rainInfoAnaStationDTO.setMinValues(minDays);
                    rainInfoAnaStationDTO.setMaxValues(maxDays);
//                    rainInfoAnaStationDTO.setMinHistoryAvgRate(0d);
//                    rainInfoAnaStationDTO.setMaxHistoryAvgRate(0d);

                    BigDecimal b1 = new BigDecimal(Double.toString(v));
                    BigDecimal b2 = new BigDecimal(Double.toString(nowDays));
                    BigDecimal b3 = new BigDecimal(Double.toString(maxDays));
                    BigDecimal b4 = new BigDecimal(Double.toString(minDays));
                    BigDecimal subtract = b2.subtract(b1);
                    BigDecimal subtract3 = b3.subtract(b1);
                    BigDecimal subtract4 = b4.subtract(b1);
//                    double v1 = subtract.divide(b1, 1, BigDecimal.ROUND_HALF_UP).doubleValue();
//                    double v3 = subtract3.divide(b1, 1, BigDecimal.ROUND_HALF_UP).doubleValue();
//                    double v4 = subtract4.divide(b1, 1, BigDecimal.ROUND_HALF_UP).doubleValue();
                    //不用计算距平了无效的
                    if (Objects.equals(nowDays,999999d)){
                        rainInfoAnaStationDTO.setMaxHistoryAvgRate(999999d);
                        rainInfoAnaStationDTO.setMinHistoryAvgRate(999999d);
                        rainInfoAnaStationDTO.setHistoryAvgRate(999999d);
                        rainInfoAnaStationDTO.setRangeNum(999999);
                    }else{
                        rainInfoAnaStationDTO.setHistoryAvgRate(NumUtil.save1Point(subtract.doubleValue()));
                        rainInfoAnaStationDTO.setMaxHistoryAvgRate(NumUtil.save1Point(subtract3.doubleValue()));
                        rainInfoAnaStationDTO.setMinHistoryAvgRate(NumUtil.save1Point(subtract4.doubleValue()));
                        //排位
                        List<RainInfoAnalysDTO> collect3 = rainInfoAnalysDTOList.stream().filter((p) -> (p.getCountDays() >= nowDays)).collect(Collectors.toList());
                        rainInfoAnaStationDTO.setRangeNum(org.apache.commons.collections4.CollectionUtils.isEmpty(collect3) ? 1 : collect3.size());
                    }
                }
            }
            singStationInfos.add(rainInfoAnaStationDTO);
        });
        //柱状图
        Map<String, List<RainInfoAnalysDTO>> collect3 = rainInfoAnalysDTOS.stream().collect(Collectors.groupingBy(RainInfoAnalysDTO::getYear));
        List<YearDataValuesDTO> yearDataValuesDTOS = new ArrayList<>();
        //循环startyear 到 endyear
        //todo 筛选最近的三十年
        List<RainInfoAnalysDTO> latest30 = rainInfoAnalysDTOS.stream()
                .filter((p) -> (Integer.parseInt(p.getYear()) >= PreYear30Enum.PRE_YEAR_30_START.getYear() && Integer.parseInt(p.getYear()) <= PreYear30Enum.PRE_YEAR_30_END.getYear())).collect(Collectors.toList());
        for (int i = Integer.parseInt(leftTime.substring(0, 4)); i <= rightYear; i++) {
            YearDataValuesDTO yearDataValuesDTO = new YearDataValuesDTO();
            yearDataValuesDTO.setYear(String.valueOf(i));
            List<RainInfoAnalysDTO> rainInfoAnalysDTOList = collect3.get(String.valueOf(i));
            if (CollectionUtils.isEmpty(rainInfoAnalysDTOList)){
                yearDataValuesDTO.setDataValues(0d);
            }else{
                DoubleSummaryStatistics collect1 = rainInfoAnalysDTOList.stream().collect(Collectors.summarizingDouble(RainInfoAnalysDTO::getCountDays));
                yearDataValuesDTO.setDataValues(NumUtil.save1Point(collect1.getSum()/stations.size()));
            }
            //每一年都是空的
            if (CollectionUtils.isEmpty(rainInfoAnalysDTOS)){
                yearDataValuesDTO.setAnomalyPercent(0d);
            }else{
                if (CollectionUtils.isEmpty(latest30)){
                    yearDataValuesDTO.setAnomalyPercent(NumUtil.save1Point(new BigDecimal(yearDataValuesDTO.getDataValues()).subtract(new BigDecimal("0")).doubleValue()));
                }else{
                    DoubleSummaryStatistics collect2 = latest30.stream().collect(Collectors.summarizingDouble(RainInfoAnalysDTO::getCountDays));
                    double v = collect2.getSum() / stations.size() / 30;
                    yearDataValuesDTO.setAnomalyPercent(NumUtil.save1Point(new BigDecimal(yearDataValuesDTO.getDataValues()).subtract(new BigDecimal(v)).doubleValue()));
                }
            }
            yearDataValuesDTOS.add(yearDataValuesDTO);
        }
        Collections.sort(yearDataValuesDTOS,((o1, o2) -> {
            return (int)(o1.getDataValues()*100 - o2.getDataValues()*100);
        }));
        for (int i = 0; i < yearDataValuesDTOS.size(); i++) {
            YearDataValuesDTO yearDataValuesDTO = yearDataValuesDTOS.get(i);
            yearDataValuesDTO.setHistoryRange(yearDataValuesDTOS.size() - i);
        }
        //按照时间排序返回前端
        Collections.sort(yearDataValuesDTOS,((o1, o2) -> {
            return Integer.parseInt(o1.getYear()) - Integer.parseInt(o2.getYear());
        }));
        RainInfoAnalysisVO rainInfoAnalysisVO = new RainInfoAnalysisVO();
        rainInfoAnalysisVO.setSingStationInfos(singStationInfos);
        rainInfoAnalysisVO.setYearDataValuesDTOS(yearDataValuesDTOS);
        if (CollectionUtils.isEmpty(rainInfoAnalysDTOS)){
            rainInfoAnalysisVO.setAvg30(0d);
        }else{
            if (CollectionUtils.isEmpty(latest30)){
                rainInfoAnalysisVO.setAvg30(0d);
            }else{
                DoubleSummaryStatistics collect2 = latest30.stream().collect(Collectors.summarizingDouble(RainInfoAnalysDTO::getCountDays));
                double v = collect2.getSum() / stations.size() / 30;
                rainInfoAnalysisVO.setAvg30(NumUtil.save1Point(v));
            }
        }
        log.info("调用用时{}",System.currentTimeMillis() - st );
        return ResultVOUtil.success(rainInfoAnalysisVO);
    }

    private List<RainInfoAnalysDTO> getRainInfoAnalysDTOS(String startTime, String endTime, String leftTime, String rightTime, String type, String flag20, String flag08, String stationNum) {
        EsAggCondition esAggCondition = EsAggCondition.getInstance();
        Group stationNoGroup = esAggCondition.Group("stationNo");

        stationNoGroup.size(10000);
        Group yearGroup = stationNoGroup.groupSub("year");
        yearGroup.count("time","countDays");
        yearGroup.size(1000);
        EsCondition esCondition = esAggCondition.getCondition().GTE("time", leftTime).LTE("time", rightTime)
                .gte("days", startTime.substring(4, 8)).lte(endTime.substring(4, 8));
        //单站的信息
        if (Objects.nonNull(stationNum) && !Objects.equals(stationNum,"")){
            esCondition.eq("stationNo",stationNum);
        }

        if (Objects.equals(type,"雨日")){
            esCondition.gte("rainFall",1).lte(20000);
        }else if (Objects.equals(type,"暴雨日")){
            esCondition.gte("rainFall",500).lte(20000);
        }else if (Objects.equals(type,"大暴雨日")){
            esCondition.gte("rainFall",1000).lte(20000);
        }else if (Objects.equals(type,"无雨日")){
            esCondition.eq("rainFall",0);
        } else if (Objects.equals(type,"自定义")){
            if (Objects.nonNull(flag20) && !Objects.equals(flag20,"")){
                String[] flag20Split = flag20.split(",");
                if (!Objects.equals(flag20Split[1],"xx")){
                    String firstCondition = flag20Split[0];
                    int conditionValue = Integer.parseInt(flag20Split[1]) * 10;
                    if (Objects.equals(firstCondition,"gt")){
                        esCondition.gt("rainFall",conditionValue);
                    }
                    if (Objects.equals(firstCondition,"gte")){
                        esCondition.gte("rainFall",conditionValue);
                    }
                    if (Objects.equals(firstCondition,"lt")){
                        esCondition.lt("rainFall",conditionValue);
                    }
                    if (Objects.equals(firstCondition,"lte")){
                        esCondition.lte("rainFall",conditionValue);
                    }
                }
                if (!Objects.equals(flag20Split[3],"xx")){
                    String secondCondition = flag20Split[2];
                    int conditionValue = Integer.parseInt(flag20Split[3]) * 10;
                    if (Objects.equals(secondCondition,"gt")){
                        esCondition.gt("rainFall",conditionValue);
                    }
                    if (Objects.equals(secondCondition,"gte")){
                        esCondition.gte("rainFall",conditionValue);
                    }
                    if (Objects.equals(secondCondition,"lt")){
                        esCondition.lt("rainFall",conditionValue);
                    }
                    if (Objects.equals(secondCondition,"lte")){
                        esCondition.lte("rainFall",conditionValue);
                    }
                }
            }
            if (Objects.nonNull(flag08) && !Objects.equals(flag08,"")){
                String[] flag08Split = flag08.split(",");
                if (!Objects.equals(flag08Split[1],"xx")){
                    String firstCondition = flag08Split[0];
                    int conditionValue = Integer.parseInt(flag08Split[1]) * 10;
                    if (Objects.equals(firstCondition,"gt")){
                        esCondition.gt("rainFall08",conditionValue);
                    }
                    if (Objects.equals(firstCondition,"gte")){
                        esCondition.gte("rainFall08",conditionValue);
                    }
                    if (Objects.equals(firstCondition,"lt")){
                        esCondition.lt("rainFall08",conditionValue);
                    }
                    if (Objects.equals(firstCondition,"lte")){
                        esCondition.lte("rainFall08",conditionValue);
                    }
                }
                if (!Objects.equals(flag08Split[3],"xx")){
                    String secondCondition = flag08Split[2];
                    int conditionValue = Integer.parseInt(flag08Split[3]) * 10;
                    if (Objects.equals(secondCondition,"gt")){
                        esCondition.gt("rainFall08",conditionValue);
                    }
                    if (Objects.equals(secondCondition,"gte")){
                        esCondition.gte("rainFall08",conditionValue);
                    }
                    if (Objects.equals(secondCondition,"lt")){
                        esCondition.lt("rainFall08",conditionValue);
                    }
                    if (Objects.equals(secondCondition,"lte")){
                        esCondition.lte("rainFall08",conditionValue);
                    }
                }
            }
        }
        return dayInfoRepository.selectListAgg(esAggCondition, new DataWrapper<RainInfoAnalysDTO>() {
            @Override
            public RainInfoAnalysDTO wrapper(Map<String, Object> data) {
                RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
                rainInfoAnalysDTO.setStationNo(data.get("stationNo").toString());
                rainInfoAnalysDTO.setYear(data.get("year").toString());
                rainInfoAnalysDTO.setCountDays(Double.valueOf(data.get("countDays").toString()));
                return rainInfoAnalysDTO;
            }
        });
    }

    /**
     * 情报分析降雨量接口
     * @param startTime
     * @param endTime
     * @param leftTime
     * @param rightTime
     * @param type
     * @return
     */
    @Override
    public ResultVO intelligence(String startTime, String endTime, String leftTime, String rightTime, String type, String stationNum) {
        Long st = System.currentTimeMillis();
        //站点基础信息
        Map<String, Station> stationInfoMap = StationCache.getStationInfoMap();
        List<RainInfoAnalysDTO> rainInfoAnalysDTOS = new ArrayList<>();
        //todo 从es查询数据 注意跨年的情况
        Integer rightYear = null;
        if (Objects.equals(startTime.substring(0,4),endTime.substring(0,4))){
            //无跨年数据直接处理返回
            rightYear = Integer.parseInt(rightTime.substring(0,4));
            rainInfoAnalysDTOS = getRainInfoAnalysDTOS(startTime, endTime, leftTime, rightTime, type, stationNum);
        }else{
            //处理跨年数据 需要分两段查询 最后进行拼接
            rightYear = Integer.parseInt(startTime.substring(0,4));
            List<RainInfoAnalysDTO> leftRainInfoAnalysDTOS = getRainInfoAnalysDTOS(startTime, rightYear + "1231", leftTime, rightYear + "1231", type, stationNum);
            List<RainInfoAnalysDTO> rightRainInfoAnalysDTOS = getRainInfoAnalysDTOS(endTime.substring(0,4) + "0101", endTime, leftTime, rightTime, type, stationNum);

            //组装跨年的数据
            rainInfoAnalysDTOS = handleRainInfoAnalysDTO(leftRainInfoAnalysDTOS, rightRainInfoAnalysDTOS, leftTime.substring(0,4), endTime.substring(0,4));

        }

        log.info("情报分析查询降雨量查询es用时{}",System.currentTimeMillis() - st);
        Map<String, Map<String, List<RainInfoAnalysDTO>>> rainInfoMapByStationAndYear = rainInfoAnalysDTOS.stream().collect(Collectors.groupingBy(RainInfoAnalysDTO::getStationNo, Collectors.groupingBy(RainInfoAnalysDTO::getYear)));

        //todo 很重要查询 当年有多少有效站点数  后期看需不需要改 暂时注释
//        Map<String, Integer> yearStationMap = new HashMap<>();
//        Map<String, List<RainInfoAnalysDTO>> yearStInfos = rainInfoAnalysDTOS.stream().collect(Collectors.groupingBy(RainInfoAnalysDTO::getYear));
//        yearStInfos.forEach((year,stationInfos) ->{
//            Set<String> stationNums = stationInfos.stream().map(RainInfoAnalysDTO::getStationNo).collect(Collectors.toSet());
//            yearStationMap.put(year,stationNums.size());
//        });

        List<RainInfoAnaStationDTO> singStationInfos = new ArrayList<>();

        //单站的信息
        if (Objects.nonNull(stationNum)){
            Map<String, List<RainInfoAnalysDTO>> singleRainInfoMapByYear = rainInfoMapByStationAndYear.get(stationNum);
            List<YearDataValuesDTO> yearDataValuesDTOS = new ArrayList<>();
            if (CollectionUtils.isEmpty(singleRainInfoMapByYear)){
                for (int i = Integer.parseInt(leftTime.substring(0, 4)); i <= Integer.parseInt(rightTime.substring(0, 4)); i++) {
                    YearDataValuesDTO yearDataValuesDTO = new YearDataValuesDTO();
                    yearDataValuesDTO.setYear(String.valueOf(i));
                    yearDataValuesDTO.setDataValues(999999d);
                    yearDataValuesDTO.setAvg30(999999d);
                    yearDataValuesDTO.setAnomalyPercent(999999d);
                    yearDataValuesDTO.setHistoryRange(999999);
                    yearDataValuesDTOS.add(yearDataValuesDTO);
                }
                return ResultVOUtil.success(yearDataValuesDTOS);
            }
            //不为空
            for (int i = Integer.parseInt(leftTime.substring(0, 4)); i <= rightYear; i++) {
                YearDataValuesDTO yearDataValuesDTO = new YearDataValuesDTO();
                yearDataValuesDTO.setYear(String.valueOf(i));
                //单站30年
                DoubleSummaryStatistics singleRainInfoAvg30Sum = rainInfoAnalysDTOS.stream().filter((p) -> (Integer.parseInt(p.getYear()) >= PreYear30Enum.PRE_YEAR_30_START.getYear() &&
                        Integer.parseInt(p.getYear()) <= PreYear30Enum.PRE_YEAR_30_END.getYear()))
                        .collect(Collectors.summarizingDouble(RainInfoAnalysDTO::getRainYearSum));
                double avg30PreStation = singleRainInfoAvg30Sum.getAverage();
                //30年平均
                BigDecimal b1 = new BigDecimal(Double.toString(avg30PreStation));
                //当前年实况值
                List<RainInfoAnalysDTO> rainInfoAnalysDTOList = singleRainInfoMapByYear.get(String.valueOf(i));
                Double nowYearRainSum;
                if (CollectionUtils.isEmpty(rainInfoAnalysDTOList)){
                    nowYearRainSum = 999999d;
                    yearDataValuesDTO.setAnomalyPercent(999999d);
                }else{
                    DoubleSummaryStatistics collect1 = rainInfoAnalysDTOList.stream().collect(Collectors.summarizingDouble(RainInfoAnalysDTO::getRainYearSum));
                    nowYearRainSum = collect1.getSum();
                    BigDecimal b2 = new BigDecimal(Double.toString(nowYearRainSum));
                    BigDecimal subtract = b2.subtract(b1);
                    if (b1.compareTo(new BigDecimal(0)) == 0){
                        yearDataValuesDTO.setAnomalyPercent(999999d);
                    }else{
                        double v = subtract.divide(b1, 3, BigDecimal.ROUND_HALF_UP).doubleValue();
                        yearDataValuesDTO.setAnomalyPercent(NumUtil.save2Point(v));
                    }

                }
                yearDataValuesDTO.setDataValues(NumUtil.save2Point(nowYearRainSum));
                yearDataValuesDTO.setAvg30(NumUtil.save2Point(avg30PreStation));
                yearDataValuesDTOS.add(yearDataValuesDTO);
            }
            //按照实况值排序
            Collections.sort(yearDataValuesDTOS,((o1, o2) -> {
                return (int)(o1.getDataValues()*100 - o2.getDataValues()*100);
            }));
            //排名信息 可能有无效的数据出现 先过滤无效的数据
            yearDataValuesDTOS = yearDataValuesDTOS.stream().filter((p) -> (p.getDataValues() < 999999)).collect(Collectors.toList());
            for (int i = 0; i < yearDataValuesDTOS.size(); i++) {
                YearDataValuesDTO yearDataValuesDTO = yearDataValuesDTOS.get(i);
                yearDataValuesDTO.setHistoryRange(yearDataValuesDTOS.size() - i);
            }

            //按照时间排序返回前端
            Collections.sort(yearDataValuesDTOS,((o1, o2) -> {
                return Integer.parseInt(o1.getYear()) - Integer.parseInt(o2.getYear());
            }));
            return ResultVOUtil.success(yearDataValuesDTOS);
        }

        //这里要循环所有的站点信息
        stationInfoMap.forEach((stationNo,stationInfo) -> {
            RainInfoAnaStationDTO rainInfoAnaStationDTO = new RainInfoAnaStationDTO();
            rainInfoAnaStationDTO.setLat(Double.valueOf(stationInfo.getLat()));
            rainInfoAnaStationDTO.setLon(Double.valueOf(stationInfo.getLon()));
            rainInfoAnaStationDTO.setStationNo(stationInfo.getStationNo());
            rainInfoAnaStationDTO.setStationName(stationInfo.getCity());
            rainInfoAnaStationDTO.setProvince(stationInfo.getProvince());
            rainInfoAnaStationDTO.setObserDate(startTime.substring(0,4));
            //当前站点实况值 还要去查下年
            Map<String, List<RainInfoAnalysDTO>> rainInfoMapByYear = rainInfoMapByStationAndYear.get(stationNo);
            if (CollectionUtils.isEmpty(rainInfoMapByYear)){
                //说明站点信息都没有直接全部给0d
                rainInfoAnaStationDTO.setValues(999999d);
                rainInfoAnaStationDTO.setMaxValues(999999d);
                rainInfoAnaStationDTO.setMinValues(999999d);
                rainInfoAnaStationDTO.setAvg30(999999d);
                rainInfoAnaStationDTO.setHistoryAvgRate(999999d);
                rainInfoAnaStationDTO.setMaxHistoryAvgRate(999999d);
                rainInfoAnaStationDTO.setMinHistoryAvgRate(999999d);
                rainInfoAnaStationDTO.setRangeNum(999999);
            }else{
                //查实况值
                List<RainInfoAnalysDTO> rainInfoAnalysDTOS1 = rainInfoMapByYear.get(startTime.substring(0, 4));
                Double rainYearSum;
                if (CollectionUtils.isEmpty(rainInfoAnalysDTOS1)){
                    rainYearSum = 999999d;
                }else{
                    rainYearSum = rainInfoAnalysDTOS1.get(0).getRainYearSum();
                }

                List<RainInfoAnalysDTO> rainInfoAnalysDTOList = new ArrayList<>();
                rainInfoMapByYear.forEach((year,value1) -> {
                    rainInfoAnalysDTOList.addAll(value1);
                });

                Collections.sort(rainInfoAnalysDTOList,((o1, o2) -> {
                    return new BigDecimal((o1.getRainYearSum()*10 - o2.getRainYearSum()*10)).intValue();
                }));

                //三十年平均值
                DoubleSummaryStatistics sigleStation30Sum = rainInfoAnalysDTOList.stream().filter((p) -> (Integer.parseInt(p.getYear()) >= PreYear30Enum.PRE_YEAR_30_START.getYear() &&
                        Integer.parseInt(p.getYear()) <= PreYear30Enum.PRE_YEAR_30_END.getYear()))
                        .collect(Collectors.summarizingDouble(RainInfoAnalysDTO::getRainYearSum));
                double avg30PreStation = sigleStation30Sum.getAverage();

                rainInfoAnaStationDTO.setValues(NumUtil.save2Point(rainYearSum));
                rainInfoAnaStationDTO.setAvg30(NumUtil.save2Point(avg30PreStation));
                rainInfoAnaStationDTO.setMaxValues(NumUtil.save2Point(rainInfoAnalysDTOList.get(rainInfoAnalysDTOList.size()-1).getRainYearSum()));
                rainInfoAnaStationDTO.setMinValues(NumUtil.save2Point(rainInfoAnalysDTOList.get(0).getRainYearSum()));

                //多数据进行排序
                BigDecimal b1 = new BigDecimal(Double.toString(avg30PreStation));
                BigDecimal b2 = new BigDecimal(Double.toString(rainYearSum));
                BigDecimal b3 = new BigDecimal(Double.toString(rainInfoAnalysDTOList.get(rainInfoAnalysDTOList.size()-1).getRainYearSum()));
                BigDecimal b4 = new BigDecimal(Double.toString(rainInfoAnalysDTOList.get(0).getRainYearSum()));
                BigDecimal subtract = b2.subtract(b1);
                BigDecimal subtract3 = b3.subtract(b1);
                BigDecimal subtract4 = b4.subtract(b1);
                double v = 0d;
                double v3 = 0d;
                double v4 = 0d;
                if (b1.compareTo(new BigDecimal(0)) == 0){
                    rainInfoAnaStationDTO.setHistoryAvgRate(999999d);
                    rainInfoAnaStationDTO.setMaxHistoryAvgRate(999999d);
                    rainInfoAnaStationDTO.setMinHistoryAvgRate(999999d);
                }else{
                    v = subtract.divide(b1, 3, BigDecimal.ROUND_HALF_UP).doubleValue();
                    v3 = subtract3.divide(b1, 3, BigDecimal.ROUND_HALF_UP).doubleValue();
                    v4 = subtract4.divide(b1, 3, BigDecimal.ROUND_HALF_UP).doubleValue();
                    if (Objects.equals(rainYearSum,999999d)){
                        rainInfoAnaStationDTO.setHistoryAvgRate(999999d);
                    }else{
                        rainInfoAnaStationDTO.setHistoryAvgRate(NumUtil.save2Point(v * 100));
                    }
                    rainInfoAnaStationDTO.setMaxHistoryAvgRate(NumUtil.save2Point(v3 * 100));
                    rainInfoAnaStationDTO.setMinHistoryAvgRate(NumUtil.save2Point(v4 * 100));
                }

//                }
                //排位
                List<RainInfoAnalysDTO> collect3 = rainInfoAnalysDTOList.stream().filter((p) -> (p.getRainYearSum() >= rainYearSum)).collect(Collectors.toList());
                rainInfoAnaStationDTO.setRangeNum(collect3.size() +1);
            }
            singStationInfos.add(rainInfoAnaStationDTO);
        });
        //todo 可以根据这些进行排序

        //todo 右侧柱状图信息
        // 所有站点30年信息
        DoubleSummaryStatistics allRainInfoAvg30Sum = rainInfoAnalysDTOS.stream().filter((p) -> (Integer.parseInt(p.getYear()) >= PreYear30Enum.PRE_YEAR_30_START.getYear() &&
                Integer.parseInt(p.getYear()) <= PreYear30Enum.PRE_YEAR_30_END.getYear()))
                .collect(Collectors.summarizingDouble(RainInfoAnalysDTO::getRainYearSum));
        //所有查询出来的数据按照年分组
        Map<String, List<RainInfoAnalysDTO>> allRainInfoMapByYear = rainInfoAnalysDTOS.stream().collect(Collectors.groupingBy(RainInfoAnalysDTO::getYear));

        List<YearDataValuesDTO> yearDataValuesDTOS = new ArrayList<>();
        for (int i = Integer.parseInt(leftTime.substring(0, 4)); i <= rightYear; i++) {
            YearDataValuesDTO yearDataValuesDTO = new YearDataValuesDTO();
            yearDataValuesDTO.setYear(String.valueOf(i));
            //当前年实况值
            List<RainInfoAnalysDTO> rainInfoAnalysDTOList = allRainInfoMapByYear.get(String.valueOf(i));
            Double nowYearRainSum;
            if (CollectionUtils.isEmpty(rainInfoAnalysDTOList)){
                nowYearRainSum = 999999d;
            }else{
                DoubleSummaryStatistics collect1 = rainInfoAnalysDTOList.stream().collect(Collectors.summarizingDouble(RainInfoAnalysDTO::getRainYearSum));
                nowYearRainSum = collect1.getSum()/stationInfoMap.size();
            }
            //当前年平均值
            double v1 = nowYearRainSum / stationInfoMap.size();
            //30年平均
            double v2 = allRainInfoAvg30Sum.getSum() / 30 / stationInfoMap.size();
            yearDataValuesDTO.setDataValues(NumUtil.save2Point(v1));
            BigDecimal b1 = new BigDecimal(Double.toString(v1));
            BigDecimal b2 = new BigDecimal(Double.toString(v2));
            BigDecimal subtract = b1.subtract(b2);
            if (v2 == 0d){
                yearDataValuesDTO.setAnomalyPercent(999999d);
            }else{
                double v = subtract.divide(b2, 1, BigDecimal.ROUND_HALF_UP).doubleValue();
                yearDataValuesDTO.setAnomalyPercent(v*100);
            }
            yearDataValuesDTO.setDataValues(NumUtil.save2Point(nowYearRainSum));
            yearDataValuesDTOS.add(yearDataValuesDTO);
        }
        //按照实况值排序
        Collections.sort(yearDataValuesDTOS,((o1, o2) -> {
            return (int)(o1.getDataValues()*100 - o2.getDataValues()*100);
        }));
        //排名信息
        yearDataValuesDTOS = yearDataValuesDTOS.stream().filter((p) -> (p.getDataValues() < 999999)).collect(Collectors.toList());
        for (int i = 0; i < yearDataValuesDTOS.size(); i++) {
            YearDataValuesDTO yearDataValuesDTO = yearDataValuesDTOS.get(i);
            yearDataValuesDTO.setHistoryRange(yearDataValuesDTOS.size() - i);
        }

        //按照时间排序返回前端
        Collections.sort(yearDataValuesDTOS,((o1, o2) -> {
            return Integer.parseInt(o1.getYear()) - Integer.parseInt(o2.getYear());
        }));


        RainInfoAnalysisVO rainInfoAnalysisVO = new RainInfoAnalysisVO();
//        rainInfoAnalysisVO.setTotalData(NumUtil.save2Point(collect2.getSum()/collect6.size()));
//        rainInfoAnalysisVO.setRangeMax(nowYearRainAnalys.get(nowYearRainAnalys.size()-1).getRainYearSum());
//        rainInfoAnalysisVO.setRangeMaxStationName(stationInfoMap.get(nowYearRainAnalys.get(nowYearRainAnalys.size()-1).getStationNo()).getCity());
//        rainInfoAnalysisVO.setRangeMin(nowYearRainAnalys.get(0).getRainYearSum());
//        rainInfoAnalysisVO.setRangeMinStationName(stationInfoMap.get(nowYearRainAnalys.get(0).getStationNo()).getCity());
        rainInfoAnalysisVO.setAvg30(NumUtil.save2Point(allRainInfoAvg30Sum.getSum()/30/stationInfoMap.size()));
        rainInfoAnalysisVO.setSingStationInfos(singStationInfos);
        rainInfoAnalysisVO.setYearDataValuesDTOS(yearDataValuesDTOS);
        log.info("调用用时{}",System.currentTimeMillis() - st );

        return ResultVOUtil.success(rainInfoAnalysisVO);
    }

    /**
     * 处理跨年的降水数据
     * @param leftRainInfoAnalysDTOS
     * @param rightRainInfoAnalysDTOS
     * @return
     */
    private List<RainInfoAnalysDTO> handleRainInfoAnalysDTO(List<RainInfoAnalysDTO> leftRainInfoAnalysDTOS, List<RainInfoAnalysDTO> rightRainInfoAnalysDTOS,String startYear, String endYear) {
        Map<String, Map<String, List<RainInfoAnalysDTO>>> leftRainInfoMapByStationAndYear = leftRainInfoAnalysDTOS.stream().collect(Collectors.groupingBy(RainInfoAnalysDTO::getStationNo, Collectors.groupingBy(RainInfoAnalysDTO::getYear)));
        Map<String, Map<String, List<RainInfoAnalysDTO>>> rightRainInfoMapByStationAndYear = rightRainInfoAnalysDTOS.stream().collect(Collectors.groupingBy(RainInfoAnalysDTO::getStationNo, Collectors.groupingBy(RainInfoAnalysDTO::getYear)));
        //todo 可能出现左边的数据多 右边数据少 需要把这部分所有的数据合并 先转成set 在去addall 再循环这部分站点去计算
        Set<String> leftStations = leftRainInfoAnalysDTOS.stream().map(RainInfoAnalysDTO::getStationNo).collect(Collectors.toSet());
        Set<String> rightStations = rightRainInfoAnalysDTOS.stream().map(RainInfoAnalysDTO::getStationNo).collect(Collectors.toSet());
        Set<String> allStations = new HashSet<>();
        allStations.addAll(leftStations);
        allStations.addAll(rightStations);
        List<RainInfoAnalysDTO> resultList = new ArrayList<>();
        //遍历所有的站点信息
        allStations.stream().forEach(stationNo -> {
            Map<String, List<RainInfoAnalysDTO>> leftInfoMaps = leftRainInfoMapByStationAndYear.get(stationNo);
            Map<String, List<RainInfoAnalysDTO>> rightInfoMaps = rightRainInfoMapByStationAndYear.get(stationNo);
            if (CollectionUtils.isEmpty(leftInfoMaps)){
                //右边可能为空么 可能
                rightInfoMaps.forEach((year,infoMaps) -> {
                    RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
                    rainInfoAnalysDTO.setStationNo(stationNo);
                    if (Objects.nonNull(infoMaps.get(0).getYear())){
                        rainInfoAnalysDTO.setYear(String.valueOf(Integer.parseInt(year) - 1));
                    }
                    if (Objects.nonNull(infoMaps.get(0).getRainFallSum())){
                        rainInfoAnalysDTO.setRainFallSum(infoMaps.get(0).getRainFallSum());
                    }
                    if (Objects.nonNull(infoMaps.get(0).getRainYearSum())){
                        rainInfoAnalysDTO.setRainYearSum(infoMaps.get(0).getRainYearSum());
                    }
                    if (Objects.nonNull(infoMaps.get(0).getCountDays())){
                        rainInfoAnalysDTO.setCountDays(infoMaps.get(0).getCountDays());
                    }
                    resultList.add(rainInfoAnalysDTO);
                });
            }else{
                if (CollectionUtils.isEmpty(rightInfoMaps)){
                    leftInfoMaps.forEach((year,leftinfoMaps) -> {
                        RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
                        rainInfoAnalysDTO.setStationNo(stationNo);
                        if (Objects.nonNull(leftinfoMaps.get(0).getRainFallSum())){
                            rainInfoAnalysDTO.setRainFallSum(leftinfoMaps.get(0).getRainFallSum());
                        }
                        if (Objects.nonNull(leftinfoMaps.get(0).getYear())){
                            rainInfoAnalysDTO.setYear(year);
                        }
                        if (Objects.nonNull(leftinfoMaps.get(0).getRainYearSum())){
                            rainInfoAnalysDTO.setRainYearSum(leftinfoMaps.get(0).getRainYearSum());
                        }
                        if (Objects.nonNull(leftinfoMaps.get(0).getCountDays())){
                            rainInfoAnalysDTO.setCountDays(leftinfoMaps.get(0).getCountDays());
                        }
                        resultList.add(rainInfoAnalysDTO);
                    });
                }else{
                    //第二年的
                    Map<String, List<RainInfoAnalysDTO>> rightRearInfoMaps = rightRainInfoMapByStationAndYear.get(stationNo);
                    Map<String, List<RainInfoAnalysDTO>> leftRearInfoMaps = leftRainInfoMapByStationAndYear.get(stationNo);
                    //todo 循环哪一个呢 循环所有的年
                    for (int i = Integer.parseInt(startYear); i <= Integer.parseInt(endYear); i++) {
                        List<RainInfoAnalysDTO> rainInfoAnalysDTOSLeft = leftRearInfoMaps.get(String.valueOf(i));
                        List<RainInfoAnalysDTO> rainInfoAnalysDTOSRight = rightRearInfoMaps.get(String.valueOf(i + 1));
                        if (CollectionUtils.isEmpty(rainInfoAnalysDTOSLeft)){
                            if (CollectionUtils.isEmpty(rainInfoAnalysDTOSRight)){
                                continue;
                            }else{
                                RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
                                //站点
                                rainInfoAnalysDTO.setStationNo(stationNo);
                                //年和time
                                if (Objects.nonNull(rainInfoAnalysDTOSRight.get(0).getYear())){
                                    rainInfoAnalysDTO.setYear(String.valueOf(i));
                                }
                                if (Objects.nonNull(rainInfoAnalysDTOSRight.get(0).getRainFallSum())){
                                    rainInfoAnalysDTO.setRainFallSum(rainInfoAnalysDTOSRight.get(0).getRainFallSum());
                                }
                                if (Objects.nonNull(rainInfoAnalysDTOSRight.get(0).getRainYearSum())){
                                    rainInfoAnalysDTO.setRainYearSum(rainInfoAnalysDTOSRight.get(0).getRainYearSum());
                                }
                                if (Objects.nonNull(rainInfoAnalysDTOSRight.get(0).getCountDays())){
                                    rainInfoAnalysDTO.setCountDays(rainInfoAnalysDTOSRight.get(0).getCountDays());
                                }
                                resultList.add(rainInfoAnalysDTO);
                            }
                        }else{
                            if (CollectionUtils.isEmpty(rainInfoAnalysDTOSRight)){
                                RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
                                //站点
                                rainInfoAnalysDTO.setStationNo(stationNo);
                                //年和time
                                if (Objects.nonNull(rainInfoAnalysDTOSLeft.get(0).getYear())){
                                    rainInfoAnalysDTO.setYear(String.valueOf(i));
                                }
                                if (Objects.nonNull(rainInfoAnalysDTOSLeft.get(0).getRainFallSum())){
                                    rainInfoAnalysDTO.setRainFallSum(rainInfoAnalysDTOSLeft.get(0).getRainFallSum());
                                }
                                if (Objects.nonNull(rainInfoAnalysDTOSLeft.get(0).getCountDays())){
                                    rainInfoAnalysDTO.setCountDays(rainInfoAnalysDTOSLeft.get(0).getCountDays());
                                }
                                if (Objects.nonNull(rainInfoAnalysDTOSLeft.get(0).getRainYearSum())){
                                    rainInfoAnalysDTO.setRainYearSum(rainInfoAnalysDTOSLeft.get(0).getRainYearSum());
                                }
                                resultList.add(rainInfoAnalysDTO);
                            }else{
                                RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
                                //站点
                                rainInfoAnalysDTO.setStationNo(stationNo);
                                //年和time
                                if (Objects.nonNull(rainInfoAnalysDTOSRight.get(0).getYear())){
                                    rainInfoAnalysDTO.setYear(String.valueOf(i));
                                }
                                if (Objects.nonNull(rainInfoAnalysDTOSRight.get(0).getRainFallSum())){
                                    rainInfoAnalysDTO.setRainFallSum(NumUtil.save1Point(new BigDecimal(rainInfoAnalysDTOSRight.get(0).getRainFallSum()).add(new BigDecimal(rainInfoAnalysDTOSLeft.get(0).getRainFallSum())).doubleValue()));
                                }
                                if (Objects.nonNull(rainInfoAnalysDTOSRight.get(0).getRainYearSum())){
                                    rainInfoAnalysDTO.setRainYearSum(NumUtil.save1Point(new BigDecimal(rainInfoAnalysDTOSRight.get(0).getRainYearSum()).add(new BigDecimal(rainInfoAnalysDTOSLeft.get(0).getRainYearSum())).doubleValue()));
//                                    rainInfoAnalysDTO.setRainYearSum(rainInfoAnalysDTOSRight.get(0).getRainYearSum());
                                }
                                if (Objects.nonNull(rainInfoAnalysDTOSRight.get(0).getCountDays())){
                                    rainInfoAnalysDTO.setCountDays(NumUtil.save1Point(new BigDecimal(rainInfoAnalysDTOSRight.get(0).getCountDays()).add(new BigDecimal(rainInfoAnalysDTOSLeft.get(0).getCountDays())).doubleValue()));
//                                    rainInfoAnalysDTO.setCountDays(rainInfoAnalysDTOSRight.get(0).getCountDays());
                                }
                                resultList.add(rainInfoAnalysDTO);
                            }
                        }
                    }


//                    leftInfoMaps.forEach((year,values) -> {
//                        RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
//                        //站点
//                        rainInfoAnalysDTO.setStationNo(stationNo);
//                        //年和time
//                        if (Objects.nonNull(values.get(0).getYear())){
//                            rainInfoAnalysDTO.setYear(year);
//                        }
//                        if (Objects.nonNull(values.get(0).getRainFallSum())){
//                            rainInfoAnalysDTO.setRainFallSum(NumUtil.save1Point(new BigDecimal(values.get(0).getRainFallSum()).add(CollectionUtils.isEmpty(rightRearInfoMaps.get(String.valueOf(Integer.parseInt(year) + 1))) ? new BigDecimal(0d) : new BigDecimal(rightRearInfoMaps.get(String.valueOf(Integer.parseInt(year) + 1)).get(0).getRainFallSum())).doubleValue()));
//                        }
//                        if (Objects.nonNull(values.get(0).getRainYearSum())){
//                            rainInfoAnalysDTO.setRainYearSum(NumUtil.save1Point(new BigDecimal(values.get(0).getRainYearSum()).add(CollectionUtils.isEmpty(rightRearInfoMaps.get(String.valueOf(Integer.parseInt(year) + 1))) ? new BigDecimal(0d) : new BigDecimal(rightRearInfoMaps.get(String.valueOf(Integer.parseInt(year) + 1)).get(0).getRainYearSum())).doubleValue()));
//                        }
//                        if (Objects.nonNull(values.get(0).getCountDays())){
//                            rainInfoAnalysDTO.setCountDays(NumUtil.save1Point(new BigDecimal(values.get(0).getCountDays()).add(CollectionUtils.isEmpty(rightRearInfoMaps.get(String.valueOf(Integer.parseInt(year) + 1))) ? new BigDecimal(0d) : new BigDecimal(rightRearInfoMaps.get(String.valueOf(Integer.parseInt(year) + 1)).get(0).getCountDays())).doubleValue()));
//                        }
//                        resultList.add(rainInfoAnalysDTO);
//                    });

                }
            }
        });







//        leftRainInfoMapByStationAndYear.forEach((stationNo,yearInfoMaps) -> {
////            //第二年的
////            Map<String, List<RainInfoAnalysDTO>> rightRearInfoMaps = rightRainInfoMapByStationAndYear.get(stationNo);
////            yearInfoMaps.forEach((year,infoMaps) -> {
////                RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
////                //站点
////                rainInfoAnalysDTO.setStationNo(stationNo);
////                //年和time
////                if (Objects.nonNull(infoMaps.get(0).getYear())){
////                    rainInfoAnalysDTO.setYear(year);
////                }
////                if (Objects.nonNull(infoMaps.get(0).getRainFallSum())){
////                    rainInfoAnalysDTO.setRainFallSum(NumUtil.save1Point(new BigDecimal(infoMaps.get(0).getRainFallSum()).add(CollectionUtils.isEmpty(rightRearInfoMaps.get(String.valueOf(Integer.parseInt(year) + 1))) ? new BigDecimal(0d) : new BigDecimal(rightRearInfoMaps.get(String.valueOf(Integer.parseInt(year) + 1)).get(0).getRainFallSum())).doubleValue()));
////                }
////                if (Objects.nonNull(infoMaps.get(0).getRainYearSum())){
////                    rainInfoAnalysDTO.setRainYearSum(NumUtil.save1Point(new BigDecimal(infoMaps.get(0).getRainYearSum()).add(CollectionUtils.isEmpty(rightRearInfoMaps.get(String.valueOf(Integer.parseInt(year) + 1))) ? new BigDecimal(0d) : new BigDecimal(rightRearInfoMaps.get(String.valueOf(Integer.parseInt(year) + 1)).get(0).getRainYearSum())).doubleValue()));
////                }
////                resultList.add(rainInfoAnalysDTO);
////            });
////        });



        return resultList;
    }

    /**
     * 从es查询情报分析降水数据
     * @param startTime
     * @param endTime
     * @param leftTime
     * @param rightTime
     * @param type
     * @param stationNum
     * @return
     */
    private List<RainInfoAnalysDTO> getRainInfoAnalysDTOS(String startTime, String endTime, String leftTime, String rightTime, String type, String stationNum) {
        EsAggCondition esAggCondition = EsAggCondition.getInstance();
        Group stationNoGroup = esAggCondition.Group("stationNo");
        stationNoGroup.sum(RainFlagEnum.getRainFlagEnum(type).getMessage(),"rainFallSum");
//        stationNoGroup.max(RainFlagEnum.getRainFlagEnum(type).getMessage(),"maxRainFall");
//        stationNoGroup.min(RainFlagEnum.getRainFlagEnum(type).getMessage(),"minRainFall");

        stationNoGroup.size(10000);
        Group yearGroup = stationNoGroup.groupSub("year");
        yearGroup.sum(RainFlagEnum.getRainFlagEnum(type).getMessage(),"rainYearSum");
        yearGroup.size(1000);
        EsCondition condition = esAggCondition.getCondition();
        condition.GTE("time",leftTime).LTE("time",rightTime).gte("days", startTime.substring(4,8)).lte(endTime.substring(4,8))
                .lte(RainFlagEnum.getRainFlagEnum(type).getMessage(),20000);
        if (Objects.nonNull(stationNum)){
            condition.eq("stationNo",stationNum);
        }

        return dayInfoRepository.selectListAgg(esAggCondition, new DataWrapper<RainInfoAnalysDTO>() {
            @Override
            public RainInfoAnalysDTO wrapper(Map<String, Object> data) {
                RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
                rainInfoAnalysDTO.setStationNo(data.get("stationNo").toString());
                rainInfoAnalysDTO.setYear(data.get("year").toString());
                rainInfoAnalysDTO.setRainYearSum(Double.parseDouble(data.get("rainYearSum").toString())/10);
                rainInfoAnalysDTO.setRainFallSum(Double.parseDouble(data.get("rainFallSum").toString())/10);
//                rainInfoAnalysDTO.setMaxRainFall(Double.parseDouble(data.get("maxRainFall").toString())/10);
//                rainInfoAnalysDTO.setMinRainFall(Double.parseDouble(data.get("minRainFall").toString())/10);
                return rainInfoAnalysDTO;
            }
        });
    }


    /**
     * 极值排位
     * @param startTime
     * @param endTime
     * @param leftTime
     * @param rightTime
     * @param type
     * @return
     */
    @Override
    public ResultVO extremeSumRain(String startTime, String endTime, Integer continueDay, String leftTime, String rightTime, String flag, String type, String stationNum) {
        Long st = System.currentTimeMillis();
        String startDate = "";
        String endDate = "";
        if (Objects.equals(type,"旬排位")){
            startDate = TimeUtil.getTenDayStartTime(startTime);
            endDate = TimeUtil.getTenDayEndTime(startTime);
        }
        if (Objects.equals(type,"月排位")){
            startDate = TimeUtil.monthStartTime(startTime);
            endDate = TimeUtil.monthEndTime(startTime);
        }
        if (Objects.equals(type,"季排位")){
            startDate = TimeUtil.getQuarterStartTime(startTime);
            endDate = TimeUtil.getQuarterEndTime(startTime);
        }
        if (Objects.equals(type,"历史排位")){
            startDate = "20190101";
            endDate = "20191231";
        }
        if (Objects.equals(type,"自定义排位")){
            startDate = startTime;
            endDate = endTime;
        }

        //todo 查询实况值 这里应该去判断开始时间和结束时间是否相等 相等单日降水量 不相等多日降水量 再去开始时间和结束时间相减判断是什么要素
        EsCondition timeCondition = EsCondition.getEsCondition();
        if (Objects.equals(continueDay,1)){
            timeCondition.eq("time", startTime).LTE(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
        }else{
            timeCondition.eq("time", endTime).LTE(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
        }
        timeCondition.size(10000);
        List<RainDayInfo> rainDayInfos = dayInfoRepository.selectList(timeCondition);
        //todo 后面也需要去判断开始时间结束时间是否相等取实况值
        Map<String, List<RainDayInfo>> collect1 = rainDayInfos.stream().collect(Collectors.groupingBy(RainDayInfo::getStationNo));


        //站点基础信息
        Map<String, Station> stationInfoMap = StationCache.getStationInfoMap();
//        List<String> monthBetweenDateStr = new ArrayList<>();
//        try {
//            monthBetweenDateStr = TimeUtil.getMonthBetweenDateStr(TimeUtil.preDay(startTime, 1), TimeUtil.nextDay(endTime, 1));
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        //连续多少天降水
//        int continueDay = monthBetweenDateStr.size();

        EsAggCondition esAggCondition = EsAggCondition.getInstance();
        Group stationNoGroup = esAggCondition.Group("stationNo");
        stationNoGroup.size(10000);
        Group yearGroup = stationNoGroup.groupSub("time");
        if (Objects.equals(continueDay,1)){
            yearGroup.sum(RainFlagEnum.getRainFlagEnum(flag).getMessage(),"rainFallSum");
        }else{
            yearGroup.sum(RainFlagEnum.getRainFlagEnum(flag).getMessage()+"_"+continueDay,"rainFallSum");
        }
        yearGroup.order(Order.DESC,"rainFallSum");
        yearGroup.size(50);
        EsCondition condition = esAggCondition.getCondition();
        //跨年条件
        if (Objects.equals(startTime.substring(0,4),endTime.substring(0,4))){
            condition.gte("time",leftTime).lte(rightTime)
                    .gte("days", startDate.substring(4,8)).lte(endDate.substring(4,8))
                    .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
        }else{
            condition.gte("time",leftTime).lte(rightTime)
                    .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
            condition.subSearch(EsCondition.getEsCondition().gte("days", startTime.substring(4,8)).lte("1231").or().gte("days", "0101").lte(endTime.substring(4,8)).or(), FlagConnect.AND);
        }


        if (Objects.nonNull(stationNum)){
            condition.eq("stationNo",stationNum);
        }


        List<RainInfoAnalysDTO> rainInfoAnalysDTOS = dayInfoRepository.selectListAgg(esAggCondition, new DataWrapper<RainInfoAnalysDTO>() {
            @Override
            public RainInfoAnalysDTO wrapper(Map<String, Object> data) {
                RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
                rainInfoAnalysDTO.setStationNo(data.get("stationNo").toString());
                rainInfoAnalysDTO.setTime(data.get("time").toString());
                rainInfoAnalysDTO.setRainFallSum(Double.parseDouble(data.get("rainFallSum").toString())/10);
                return rainInfoAnalysDTO;
            }
        });
        log.info("查询es用时{}",System.currentTimeMillis() - st );



        Map<String, List<RainInfoAnalysDTO>> rainInfoAnalysMapByStationNo = rainInfoAnalysDTOS.stream().collect(Collectors.groupingBy(RainInfoAnalysDTO::getStationNo));
        List<RainInfoAnaStationDTO> singStationInfos = new ArrayList<>();

        //todo 单站相关信息 这里直接return掉
        if (Objects.nonNull(stationNum)){
            double liveRainFall = getLiveRainFall(collect1, stationNum, continueDay, flag);
            if (CollectionUtils.isEmpty(rainInfoAnalysMapByStationNo)){
                RainInfoAnaStationDTO rainInfoAnaStationDTO = getNowRainInfoAnaStation(startTime, endTime, flag, stationNum, collect1, stationInfoMap, continueDay);
                singStationInfos.add(rainInfoAnaStationDTO);
                return ResultVOUtil.success(singStationInfos);
            }
            List<RainInfoAnalysDTO> rainInfoAnalysDTOList = rainInfoAnalysMapByStationNo.get(stationNum);
            for (int i = 0; i < rainInfoAnalysDTOList.size(); i++) {
                RainInfoAnalysDTO rainInfoAnalysDTO = rainInfoAnalysDTOList.get(i);
                RainInfoAnaStationDTO rainInfoAnaStationDTO = new RainInfoAnaStationDTO();
                rainInfoAnaStationDTO.setLat(Double.valueOf(stationInfoMap.get(stationNum).getLat()));
                rainInfoAnaStationDTO.setLon(Double.valueOf(stationInfoMap.get(stationNum).getLon()));
                rainInfoAnaStationDTO.setStationNo(stationInfoMap.get(stationNum).getStationNo());
                rainInfoAnaStationDTO.setStationName(stationInfoMap.get(stationNum).getCity());
                rainInfoAnaStationDTO.setProvince(stationInfoMap.get(stationNum).getProvince());
                rainInfoAnaStationDTO.setPreObserDate(TimeUtil.preDay(rainInfoAnalysDTO.getTime(),continueDay-1));
                rainInfoAnaStationDTO.setObserDate(rainInfoAnalysDTO.getTime());
                double liveRainFall1 = rainInfoAnalysDTO.getRainFallSum();
                rainInfoAnaStationDTO.setValues(liveRainFall1);
                rainInfoAnaStationDTO.setRangeNum(i + 1);
                singStationInfos.add(rainInfoAnaStationDTO);
            }
            RainInfoAnaStationDTO rainInfoAnaStationDTO = getNowRainInfoAnaStation(startTime, endTime, flag, stationNum, collect1, stationInfoMap, continueDay);
            if (Objects.equals(liveRainFall,999999d)){
                rainInfoAnaStationDTO.setRangeNum(999999);
            }else{
                List<RainInfoAnalysDTO> gtNowValues = rainInfoAnalysDTOList.stream().filter((p) -> (p.getRainFallSum() < liveRainFall)).collect(Collectors.toList());
                rainInfoAnaStationDTO.setRangeNum(CollectionUtils.isEmpty(gtNowValues) ? 999999 : rainInfoAnalysDTOList.size() - gtNowValues.size());
            }
            singStationInfos.add(singStationInfos.size(),rainInfoAnaStationDTO);
            //todo 这里还需要加当前的数据进来 看是前端拼 还是后端处理到时候看情况
            log.info("极值排位降水量接口总用时{}",System.currentTimeMillis() - st );
            return ResultVOUtil.success(singStationInfos);
        }

        //todo 循环基础数据，以防基础信息不全 如果基础信息没实况就为0
        stationInfoMap.forEach((stationNo,stationInfo) -> {
            RainInfoAnaStationDTO rainInfoAnaStationDTO = new RainInfoAnaStationDTO();
            rainInfoAnaStationDTO.setLat(Double.valueOf(stationInfo.getLat()));
            rainInfoAnaStationDTO.setLon(Double.valueOf(stationInfo.getLon()));
            rainInfoAnaStationDTO.setStationNo(stationInfo.getStationNo());
            rainInfoAnaStationDTO.setStationName(stationInfo.getCity());
            rainInfoAnaStationDTO.setProvince(stationInfo.getProvince());
            rainInfoAnaStationDTO.setPreObserDate(TimeUtil.preDay(endTime,continueDay-1));
            rainInfoAnaStationDTO.setObserDate(endTime);
            List<RainInfoAnalysDTO> rainInfoAnalysDTOList = rainInfoAnalysMapByStationNo.get(stationNo);
            double liveRainFall = getLiveRainFall(collect1, stationNo, continueDay, flag);
            if (!CollectionUtils.isEmpty(rainInfoAnalysDTOList)){
                if (Objects.equals(liveRainFall,999999d)){
                    rainInfoAnaStationDTO.setRangeNum(999999);
                }else {
                    List<RainInfoAnalysDTO> gtNowValues = rainInfoAnalysDTOList.stream().filter((p) -> (p.getRainFallSum() < liveRainFall)).collect(Collectors.toList());
                    rainInfoAnaStationDTO.setRangeNum(CollectionUtils.isEmpty(gtNowValues) ? 999999 : rainInfoAnalysDTOList.size() - gtNowValues.size());
                }
            }else{
                rainInfoAnaStationDTO.setRangeNum(999999);
            }
            rainInfoAnaStationDTO.setValues(liveRainFall);
            singStationInfos.add(rainInfoAnaStationDTO);
        });
        log.info("极值排位降水量接口总用时{}",System.currentTimeMillis() - st );
        return ResultVOUtil.success(singStationInfos);
    }


    /**
     * 连续降水过程降雨量和降水日接口
     * @param startTime
     * @param leftTime
     * @param rightTime
     * @param flag
     * @param type
     * @param elementType
     * @param sign
     * @param stationNum
     * @return
     */
    @Override
    public ResultVO contunueRain(String startTime, String endTime, String leftTime, String rightTime, String flag, String type, String elementType, String sign, String stationNum) {
        Long st = System.currentTimeMillis();
        String startDate = "";
        String endDate = "";
                        if (Objects.equals(type,"旬排位")){
            startDate = TimeUtil.getTenDayStartTime(startTime);
            endDate = TimeUtil.getTenDayEndTime(startTime);
        }
        if (Objects.equals(type,"月排位")){
            startDate = TimeUtil.monthStartTime(startTime);
            endDate = TimeUtil.monthEndTime(startTime);
        }
        if (Objects.equals(type,"季排位")){
            startDate = TimeUtil.getQuarterStartTime(startTime);
            endDate = TimeUtil.getQuarterEndTime(startTime);
        }
        if (Objects.equals(type,"历史排位")){
            startDate = "20190101";
            endDate = "20191231";
        }
        if (Objects.equals(type,"自定义排位")){
            startDate = startTime;
            endDate = endTime;
        }


        EsAggCondition esAggCondition = EsAggCondition.getInstance();
        Group stationNoGroup = esAggCondition.Group("stationNo");
        stationNoGroup.size(10000);
        Group courseGroup = null;
        if (Objects.equals(elementType,"1") && Objects.equals(flag,"20")){
            courseGroup = stationNoGroup.groupSub("continueRainFll");
            courseGroup.count("continueRainFll","continueDay");
        }else if (Objects.equals(elementType,"1") && Objects.equals(flag,"08")){
            courseGroup = stationNoGroup.groupSub("continueRainFll08");
            courseGroup.count("continueRainFll08","continueDay");
        }else if (Objects.equals(elementType,"2") && Objects.equals(flag,"08")){
            courseGroup = stationNoGroup.groupSub("notContinueRainFll08");
            courseGroup.count("notContinueRainFll08","continueDay");
        }else if (Objects.equals(elementType,"2") && Objects.equals(flag,"20")){
            courseGroup = stationNoGroup.groupSub("notContinueRainFll");
            courseGroup.count("notContinueRainFll","continueDay");
        }
        courseGroup.min("time");
        courseGroup.sum(RainFlagEnum.getRainFlagEnum(flag).getMessage(),"rainFallSum");
        if (Objects.equals(sign,"sum")){
            courseGroup.order(Order.DESC,"rainFallSum");
        }else if (Objects.equals(sign,"day")){
            courseGroup.order(Order.DESC,"continueDay");
        }
        courseGroup.size(50);
        EsCondition condition = esAggCondition.getCondition();

        if (Objects.equals(startTime.substring(0,4),endTime.substring(0,4))){
            if (Objects.equals(elementType,"1") && Objects.equals(flag,"20")){
                condition.gte("time",leftTime).lte(rightTime)
                        .eq("continueRainFll",null).not()
                        .gte("days", startDate.substring(4,8)).lte(endDate.substring(4,8))
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
            }else if (Objects.equals(elementType,"1") && Objects.equals(flag,"08")){
                condition.gte("time",leftTime).lte(rightTime)
                        .eq("continueRainFll08",null).not()
                        .gte("days", startDate.substring(4,8)).lte(endDate.substring(4,8))
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
            }else if (Objects.equals(elementType,"2") && Objects.equals(flag,"08")){
                condition.gte("time",leftTime).lte(rightTime)
                        .eq("notContinueRainFll08",null).not()
                        .gte("days", startDate.substring(4,8)).lte(endDate.substring(4,8))
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
            }else if (Objects.equals(elementType,"2") && Objects.equals(flag,"20")){
                condition.gte("time",leftTime).lte(rightTime)
                        .eq("notContinueRainFll",null).not()
                        .gte("days", startDate.substring(4,8)).lte(endDate.substring(4,8))
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
            }
        }else{

            if (Objects.equals(elementType,"1") && Objects.equals(flag,"20")){
                condition.gte("time",leftTime).lte(rightTime)
                        .eq("continueRainFll",null).not()
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
                condition.subSearch(EsCondition.getEsCondition().gte("days", startTime.substring(4,8)).lte("1231").or().gte("days", "0101").lte(endTime.substring(4,8)).or(), FlagConnect.AND);
            }else if (Objects.equals(elementType,"1") && Objects.equals(flag,"08")){
                condition.gte("time",leftTime).lte(rightTime)
                        .eq("continueRainFll08",null).not()
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
                condition.subSearch(EsCondition.getEsCondition().gte("days", startTime.substring(4,8)).lte("1231").or().gte("days", "0101").lte(endTime.substring(4,8)).or(), FlagConnect.AND);
            }else if (Objects.equals(elementType,"2") && Objects.equals(flag,"08")){
                condition.gte("time",leftTime).lte(rightTime)
                        .eq("notContinueRainFll08",null).not()
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
                condition.subSearch(EsCondition.getEsCondition().gte("days", startTime.substring(4,8)).lte("1231").or().gte("days", "0101").lte(endTime.substring(4,8)).or(), FlagConnect.AND);
            }else if (Objects.equals(elementType,"2") && Objects.equals(flag,"20")){
                condition.gte("time",leftTime).lte(rightTime)
                        .eq("notContinueRainFll",null).not()
                        .lte(RainFlagEnum.getRainFlagEnum(flag).getMessage(),20000);
                condition.subSearch(EsCondition.getEsCondition().gte("days", startTime.substring(4,8)).lte("1231").or().gte("days", "0101").lte(endTime.substring(4,8)).or(), FlagConnect.AND);
            }
        }

        if (Objects.nonNull(stationNum)){
            condition.eq("stationNo",stationNum);
        }
        List<RainInfoAnalysDTO> rainInfoAnalysDTOS = dayInfoRepository.selectListAgg(esAggCondition, new DataWrapper<RainInfoAnalysDTO>() {
            @Override
            public RainInfoAnalysDTO wrapper(Map<String, Object> data) {
//                if(Objects.equals(data.get("continueDay").toString(),"1") ){
//                    return null;
//                }
                RainInfoAnalysDTO rainInfoAnalysDTO = new RainInfoAnalysDTO();
                rainInfoAnalysDTO.setStationNo(data.get("stationNo").toString());
                if (Objects.equals(elementType,"1") && Objects.equals(flag,"20")){
                    rainInfoAnalysDTO.setCourseFlag(data.get("continueRainFll").toString());
                }else if (Objects.equals(elementType,"1") && Objects.equals(flag,"08")){
                    rainInfoAnalysDTO.setCourseFlag(data.get("continueRainFll08").toString());
                }else if (Objects.equals(elementType,"2") && Objects.equals(flag,"08")){
                    rainInfoAnalysDTO.setCourseFlag(data.get("notContinueRainFll08").toString());
                }else if (Objects.equals(elementType,"2") && Objects.equals(flag,"20")){
                    rainInfoAnalysDTO.setCourseFlag(data.get("notContinueRainFll").toString());
                }
                rainInfoAnalysDTO.setTime(String.valueOf(new Double(data.get("time").toString()).intValue()));
//                rainInfoAnalysDTO.setTime(String.valueOf(new BigDecimal(data.get("time").toString()).intValue()));
                rainInfoAnalysDTO.setCountDays(Double.valueOf(data.get("continueDay").toString()));
                rainInfoAnalysDTO.setRainFallSum(Double.parseDouble(data.get("rainFallSum").toString())/10);
                return rainInfoAnalysDTO;
            }
        });
        log.info("查询es用时{}",System.currentTimeMillis() - st );


        String finalEndDate = endDate;
        String finalStartDate = startDate;
        //处理开始时间是最后一天但是
        List<RainInfoAnalysDTO> rainInfoAnalysDTOList = new ArrayList<>();
        if (Objects.equals(startTime.substring(0,4),endTime.substring(0,4))){
            rainInfoAnalysDTOList = rainInfoAnalysDTOS.stream()
                    .filter((p) -> (!Objects.equals(p.getTime().substring(4, 8), finalEndDate.substring(4, 8)))).collect(Collectors.toList());
        }else{
            rainInfoAnalysDTOList = rainInfoAnalysDTOS.stream()
                    .filter((p) -> (!Objects.equals(p.getTime().substring(4, 8), endTime.substring(4, 8)))).collect(Collectors.toList());
        }


        Map<String, List<RainInfoAnalysDTO>> rainInfoAnalysMapByStationNo = rainInfoAnalysDTOList.stream()
                .collect(Collectors.groupingBy(RainInfoAnalysDTO::getStationNo));
        List<RainInfoAnaStationDTO> singStationInfos = new ArrayList<>();
        //todo 这里等下判断 stationNum是不是为空 判断是什么接口
        //todo 循环的应该是基础信息站点
        //todo 循环基础数据，以防基础信息不全 如果基础信息没实况就为
        Map<String, Station> stationInfoMap = StationCache.getStationInfoMap();
        //todo 单站相关信息 这里直接return掉
        if (Objects.nonNull(stationNum)){
            List<RainInfoAnalysDTO> rainInfoAnalysDTOList1 = rainInfoAnalysMapByStationNo.get(stationNum);

            if (CollectionUtils.isEmpty(rainInfoAnalysMapByStationNo)){
                RainInfoAnaStationDTO rainInfoAnaStationDTO = new RainInfoAnaStationDTO();
                rainInfoAnaStationDTO.setLat(Double.valueOf(stationInfoMap.get(stationNum).getLat()));
                rainInfoAnaStationDTO.setLon(Double.valueOf(stationInfoMap.get(stationNum).getLon()));
                rainInfoAnaStationDTO.setStationNo(stationInfoMap.get(stationNum).getStationNo());
                rainInfoAnaStationDTO.setStationName(stationInfoMap.get(stationNum).getCity());
                rainInfoAnaStationDTO.setProvince(stationInfoMap.get(stationNum).getProvince());
                rainInfoAnaStationDTO.setValues(999999d);
                rainInfoAnaStationDTO.setContinueDays(999999);
                rainInfoAnaStationDTO.setPreObserDate(startTime);
                rainInfoAnaStationDTO.setObserDate(startTime);
                rainInfoAnaStationDTO.setRangeNum(999999);
                singStationInfos.add(rainInfoAnaStationDTO);
                return ResultVOUtil.success(singStationInfos);
            }
            for (int i = 0; i < rainInfoAnalysDTOList1.size(); i++) {
                RainInfoAnalysDTO rainInfoAnalysDTO = rainInfoAnalysDTOList.get(i);
                RainInfoAnaStationDTO rainInfoAnaStationDTO = new RainInfoAnaStationDTO();
                rainInfoAnaStationDTO.setLat(Double.valueOf(stationInfoMap.get(stationNum).getLat()));
                rainInfoAnaStationDTO.setLon(Double.valueOf(stationInfoMap.get(stationNum).getLon()));
                rainInfoAnaStationDTO.setStationNo(stationInfoMap.get(stationNum).getStationNo());
                rainInfoAnaStationDTO.setStationName(stationInfoMap.get(stationNum).getCity());
                rainInfoAnaStationDTO.setProvince(stationInfoMap.get(stationNum).getProvince());
                rainInfoAnaStationDTO.setPreObserDate(rainInfoAnalysDTO.getTime());
                rainInfoAnaStationDTO.setObserDate(TimeUtil.nextDay(rainInfoAnalysDTO.getTime(),rainInfoAnalysDTO.getCountDays().intValue()-1));
                rainInfoAnaStationDTO.setValues(rainInfoAnalysDTO.getRainFallSum());
                rainInfoAnaStationDTO.setContinueDays(rainInfoAnalysDTO.getCountDays().intValue());
                rainInfoAnaStationDTO.setRangeNum(i + 1);
                singStationInfos.add(rainInfoAnaStationDTO);
            }
            RainInfoAnaStationDTO rainInfoAnaStationDTO = getNowDateRainInfoAnaStationDTO(startTime, finalEndDate, finalStartDate, rainInfoAnalysMapByStationNo, stationNum, stationInfoMap.get(stationNum));
            singStationInfos.add(singStationInfos.size(),rainInfoAnaStationDTO);
            log.info("极值排位降水量接口总用时{}",System.currentTimeMillis() - st );
            return ResultVOUtil.success(singStationInfos);
        }

        stationInfoMap.forEach((stationNo,stationInfo) -> {
            RainInfoAnaStationDTO rainInfoAnaStationDTO = getNowDateRainInfoAnaStationDTO(startTime, finalEndDate, finalStartDate, rainInfoAnalysMapByStationNo, stationNo, stationInfo);
            singStationInfos.add(rainInfoAnaStationDTO);
        });
        log.info("极值接口总用时{}",System.currentTimeMillis() - st );
        return ResultVOUtil.success(singStationInfos);
    }

    /**
     * 分级降水 占比与频率统计
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param leftYear 查询范围左
     * @param rightYear 查询范围右
     * @param flag 08 or 20
     * @param type 占比：1 频率：2
     * @param stationNo 站点id
     * @param rangeParms
     */
    @Override
    public List<RainDayRangeVO> rangeGroup(String startTime, String endTime, String leftYear, String rightYear, String flag, String type, String stationNo, List<RangeParm> rangeParms) {
        // 获取查询要素
        String rainFallFlag = getRainFallFlag(flag);
        long st = System.currentTimeMillis();
        EsAggCondition esAggCondition = EsAggCondition.getInstance();
        // 根据站点分组
        Group stationNoGroup = esAggCondition.Group("stationNo");
        stationNoGroup.size(10000);
        // 根据时间分组
        Group yearGroup = stationNoGroup.groupSub("year", "yearSub");
        if (Objects.equals("1",type)){
            // 占比
            yearGroup.sum(rainFallFlag,"totalValue");
        }else{
            // 频率
            yearGroup.count("time","totalValue");
        }
        yearGroup.size(1000);
        // 分级降水分组
        RFFM[] rffms1 = rainHourService.getRffm(rangeParms);
        Group rangeSub = yearGroup.rangeSub(rainFallFlag, "rainFallFlag", rffms1);
        if (Objects.equals("1",type)){
            // 占比
            rangeSub.sum(rainFallFlag,"subValue");
        }else{
            // 频率
            rangeSub.count("time","subValue");
        }
        rangeSub.size(rangeParms.size());
        // es条件封装
        EsCondition condition = esAggCondition.getCondition();
        if (Objects.nonNull(stationNo)){
            condition.eq("stationNo",stationNo);
        }
        condition.gte("days",startTime.substring(4,8)).lte(endTime.substring(4,8));
        condition.gte("year",leftYear).lte(rightYear);
        condition.lte(rainFallFlag,20000);//日降水不可能超过2000mm
        // 查询结果集合封装
        List<DayRangeDTO> dayRangeDTOS = dayInfoRepository.selectListAgg(esAggCondition, new DataWrapper<DayRangeDTO>() {
            @Override
            public DayRangeDTO wrapper(Map<String, Object> data) {
                DayRangeDTO dayRangeDTO = new DayRangeDTO();
                dayRangeDTO.setStationNo(data.get("stationNo").toString());
                dayRangeDTO.setRangeFlag(data.get("rainFallFlag").toString());
                dayRangeDTO.setYear(data.get("yearSub").toString());
                if (Objects.equals("1",type)){
                    dayRangeDTO.setYearValue(Double.parseDouble(data.get("totalValue").toString()) / 10);
                    dayRangeDTO.setRangeSum(Double.parseDouble(data.get("subValue").toString()) / 10);
                }else{
                    dayRangeDTO.setYearValue(Double.parseDouble(data.get("totalValue").toString()));
                    dayRangeDTO.setRangeSum(Double.parseDouble(data.get("subValue").toString()));
                }

                if (Objects.equals(0d,Double.parseDouble(data.get("totalValue").toString()))){
                    dayRangeDTO.setValue(0d);
                }else{
                    dayRangeDTO.setValue(BigDecimalArithUtil.div(dayRangeDTO.getRangeSum(), dayRangeDTO.getYearValue()));
                }
                return dayRangeDTO;
            }
        });
        log.info("查询日分级降水用时：{}",System.currentTimeMillis() - st);
        List<RainDayRangeVO> rainDayRangeVOS = new ArrayList<>();
        if (Objects.nonNull(stationNo)){
            // 单站数据返回
            transSigleDayRangeDTOS(dayRangeDTOS, rainDayRangeVOS, stationNo);
        }else{
            transDayRangeDTOS(dayRangeDTOS, rainDayRangeVOS);
        }
        return rainDayRangeVOS;
    }

    public void transSigleDayRangeDTOS(List<DayRangeDTO> dayRangeDTOS, List<RainDayRangeVO> rainDayRangeVOS, String stationNo){
        Map<String, Station> stationInfoMap = StationCache.getStationInfoMap();
        Map<String, Map<String, List<DayRangeDTO>>> stationStatisticsMap = dayRangeDTOS.stream().collect(Collectors.groupingBy(DayRangeDTO::getYear, Collectors.groupingBy(DayRangeDTO::getRangeFlag)));
        stationStatisticsMap.forEach((year, falgMap) -> {
            RainDayRangeVO.RainDayRangeVOBuilder builder = RainDayRangeVO.builder();
            builder.stationNo(stationNo);
            builder.stationName(stationInfoMap.get(stationNo).getCity());
            builder.year(year);
            builder.flag(2);
            falgMap.forEach((key,value) -> {
                if (Objects.equals(key, DayRangeEnum.XIAOYU.getFlag())){
                    builder.xiaoyuValue(value.get(0).getRangeSum());
                }
                if (Objects.equals(key, DayRangeEnum.ZHONGYU.getFlag())){
                    builder.zhongyuValue(value.get(0).getRangeSum());
                }
                if (Objects.equals(key, DayRangeEnum.DAYU.getFlag())){
                    builder.dayuValue(value.get(0).getRangeSum());
                }
                if (Objects.equals(key, DayRangeEnum.BAOYU.getFlag())){
                    builder.baoyuValue(value.get(0).getRangeSum());
                }
                if (Objects.equals(key, DayRangeEnum.DABAOYU.getFlag())){
                    builder.dabaoyuValue(value.get(0).getRangeSum());
                }
                if (Objects.equals(key, DayRangeEnum.TEDABAOYU.getFlag())){
                    builder.tedabaoyuValue(value.get(0).getRangeSum());
                }
                builder.totalValue(value.get(0).getYearValue());
            });
            rainDayRangeVOS.add(builder.build());
        });

    }

    /**
     * 多站点
     * @param dayRangeDTOS
     * @param rainDayRangeVOS
     */
    public void transDayRangeDTOS(List<DayRangeDTO> dayRangeDTOS, List<RainDayRangeVO> rainDayRangeVOS){

        Map<String, Map<String, DoubleSummaryStatistics>> yearStatisticsMap = dayRangeDTOS.stream().collect(Collectors.groupingBy(DayRangeDTO::getYear,
                Collectors.groupingBy(DayRangeDTO::getRangeFlag, Collectors.summarizingDouble(DayRangeDTO::getValue))));
        yearStatisticsMap.forEach((year,flagMap) -> {
            RainDayRangeVO.RainDayRangeVOBuilder builder = RainDayRangeVO.builder();
            builder.year(year);
            builder.flag(1);
            flagMap.forEach((key,value) -> {
                if (Objects.equals(key, DayRangeEnum.XIAOYU.getFlag())){
                    builder.xiaoyuValue(NumUtil.save4Point(value.getAverage()));
                }
                if (Objects.equals(key, DayRangeEnum.ZHONGYU.getFlag())){
                    builder.zhongyuValue(NumUtil.save4Point(value.getAverage()));
                }
                if (Objects.equals(key, DayRangeEnum.DAYU.getFlag())){
                    builder.dayuValue(NumUtil.save4Point(value.getAverage()));
                }
                if (Objects.equals(key, DayRangeEnum.BAOYU.getFlag())){
                    builder.baoyuValue(NumUtil.save4Point(value.getAverage()));
                }
                if (Objects.equals(key, DayRangeEnum.DABAOYU.getFlag())){
                    builder.dabaoyuValue(NumUtil.save4Point(value.getAverage()));
                }
                if (Objects.equals(key, DayRangeEnum.TEDABAOYU.getFlag())){
                    builder.tedabaoyuValue(NumUtil.save4Point(value.getAverage()));
                }
            });
            rainDayRangeVOS.add(builder.build());
        });



    }


    //根据 08 20 返回查询要素
    private String getRainFallFlag(String flag) {
        String rainFallFlag = null;
        if (Objects.equals("20",flag)){
            rainFallFlag = "rainFall";
        }else if (Objects.equals("08",flag)){
            rainFallFlag = "rainFall08";
        }
        return rainFallFlag;
    }

    private RainInfoAnaStationDTO getNowDateRainInfoAnaStationDTO(String startTime, String finalEndDate, String finalStartDate, Map<String, List<RainInfoAnalysDTO>> rainInfoAnalysMapByStationNo, String stationNo, Station stationInfo) {
        RainInfoAnaStationDTO rainInfoAnaStationDTO = new RainInfoAnaStationDTO();
        rainInfoAnaStationDTO.setLat(Double.valueOf(stationInfo.getLat()));
        rainInfoAnaStationDTO.setLon(Double.valueOf(stationInfo.getLon()));
        rainInfoAnaStationDTO.setStationNo(stationInfo.getStationNo());
        rainInfoAnaStationDTO.setStationName(stationInfo.getCity());
        rainInfoAnaStationDTO.setProvince(stationInfo.getProvince());
        //实况值
        List<RainInfoAnalysDTO> rainInfoAnalysDTOList1 = rainInfoAnalysMapByStationNo.get(stationNo);
        if (!CollectionUtils.isEmpty(rainInfoAnalysDTOList1)){
            //符合条件的过程

            List<RainInfoAnalysDTO> areaRainInfoAnalys = rainInfoAnalysDTOList1.stream().filter((p) -> (Integer.parseInt(p.getTime()) < Integer.parseInt(finalEndDate)
                    && Integer.parseInt(p.getTime()) > Integer.parseInt(finalStartDate))).collect(Collectors.toList());

            if (CollectionUtils.isEmpty(areaRainInfoAnalys)){
                rainInfoAnaStationDTO.setValues(999999d);
                rainInfoAnaStationDTO.setContinueDays(999999);
                rainInfoAnaStationDTO.setPreObserDate(startTime);
                rainInfoAnaStationDTO.setObserDate(startTime);
                rainInfoAnaStationDTO.setRangeNum(999999);
            }else{
                //todo 这个过程可能有很多个找一个最近的过程 小于当前时间和大于当前时间的情况
                List<RainInfoAnalysDTO> collect = new ArrayList<>();
                collect = areaRainInfoAnalys.stream().filter((p) -> (Integer.parseInt(p.getTime()) < Integer.parseInt(startTime))).collect(Collectors.toList());
                if (CollectionUtils.isEmpty(collect)){
                    collect = areaRainInfoAnalys.stream().filter((p) -> (Integer.parseInt(p.getTime()) >= Integer.parseInt(startTime))).collect(Collectors.toList());
                    Collections.sort(collect,(o1, o2) -> {
                        return Integer.parseInt(o1.getTime()) - Integer.parseInt(o2.getTime());
                    });
                }else{
                    Collections.sort(collect,(o1, o2) -> {
                        return Integer.parseInt(o2.getTime()) - Integer.parseInt(o1.getTime());
                    });
                }
                rainInfoAnaStationDTO.setValues(collect.get(0).getRainFallSum());
                rainInfoAnaStationDTO.setPreObserDate(collect.get(0).getTime());
                rainInfoAnaStationDTO.setContinueDays(collect.get(0).getCountDays().intValue());
                rainInfoAnaStationDTO.setObserDate(TimeUtil.nextDay(collect.get(0).getTime(),collect.get(0).getCountDays().intValue() - 1));
                List<RainInfoAnalysDTO> finalCollect = collect;
                List<RainInfoAnalysDTO> gtNowValues = rainInfoAnalysDTOList1.stream().filter((p) -> (p.getRainFallSum() >= finalCollect.get(0).getRainFallSum())).collect(Collectors.toList());
                rainInfoAnaStationDTO.setRangeNum(CollectionUtils.isEmpty(gtNowValues) ? 999999 : rainInfoAnalysDTOList1.size() - gtNowValues.size() + 1);
            }
        }else{
            rainInfoAnaStationDTO.setValues(999999d);
            rainInfoAnaStationDTO.setPreObserDate(startTime);
            rainInfoAnaStationDTO.setObserDate(startTime);
            rainInfoAnaStationDTO.setContinueDays(999999);
            rainInfoAnaStationDTO.setRangeNum(999999);
        }
        return rainInfoAnaStationDTO;
    }

    private RainInfoAnaStationDTO getNowRainInfoAnaStation(String startTime, String endTime, String flag, String stationNum, Map<String, List<RainDayInfo>> collect1, Map<String, Station> stationInfoMap, int continueDay) {
        double liveRainFall = getLiveRainFall(collect1, stationNum, continueDay, flag);
        RainInfoAnaStationDTO rainInfoAnaStationDTO = new RainInfoAnaStationDTO();
        rainInfoAnaStationDTO.setLat(Double.valueOf(stationInfoMap.get(stationNum).getLat()));
        rainInfoAnaStationDTO.setLon(Double.valueOf(stationInfoMap.get(stationNum).getLon()));
        rainInfoAnaStationDTO.setStationNo(stationInfoMap.get(stationNum).getStationNo());
        rainInfoAnaStationDTO.setStationName(stationInfoMap.get(stationNum).getCity());
        rainInfoAnaStationDTO.setProvince(stationInfoMap.get(stationNum).getProvince());
        rainInfoAnaStationDTO.setPreObserDate(TimeUtil.preDay(endTime,continueDay-1));
        rainInfoAnaStationDTO.setObserDate(endTime);
        rainInfoAnaStationDTO.setValues(liveRainFall);
        rainInfoAnaStationDTO.setRangeNum(999999);
        return rainInfoAnaStationDTO;
    }


    private double getLiveRainFall(Map<String, List<RainDayInfo>> collect1,String stationNo,Integer continueDay,String flag){
        if (CollectionUtils.isEmpty(collect1.get(stationNo))){
            return 999999d;
        }
        if (Objects.equals(flag,"20")){
            if (Objects.equals(continueDay,1)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall())) / 10);
            }
            if (Objects.equals(continueDay,2)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_2()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_2())) / 10);
            }
            if (Objects.equals(continueDay,3)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_3()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_3())) / 10);
            }
            if (Objects.equals(continueDay,4)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_4()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_4())) / 10);
            }
            if (Objects.equals(continueDay,5)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_5()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_5())) / 10);
            }
            if (Objects.equals(continueDay,6)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_6()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_6())) / 10);
            }
            if (Objects.equals(continueDay,7)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_7()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_7())) / 10);
            }
            if (Objects.equals(continueDay,8)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_8()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_8())) / 10);
            }
            if (Objects.equals(continueDay,9)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_9()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_9())) / 10);
            }
            if (Objects.equals(continueDay,10)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_10()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_10())) / 10);
            }
            if (Objects.equals(continueDay,11)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_11()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_11())) / 10);
            }
            if (Objects.equals(continueDay,12)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_12()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_12())) / 10);
            }
            if (Objects.equals(continueDay,13)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_13()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_13())) / 10);
            }
            if (Objects.equals(continueDay,14)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_14()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_14())) / 10);
            }
            if (Objects.equals(continueDay,15)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall_15()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall_15())) / 10);
            }
        }
        if (Objects.equals(flag,"08")){
            if (Objects.equals(continueDay,1)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08())) / 10);
            }
            if (Objects.equals(continueDay,2)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_2()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_2())) / 10);
            }
            if (Objects.equals(continueDay,3)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_3()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_3())) / 10);
            }
            if (Objects.equals(continueDay,4)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_4()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_4())) / 10);
            }
            if (Objects.equals(continueDay,5)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_5()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_5())) / 10);
            }
            if (Objects.equals(continueDay,6)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_6()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_6())) / 10);
            }
            if (Objects.equals(continueDay,7)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_7()) ? 999999d :Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_7())) / 10);
            }
            if (Objects.equals(continueDay,8)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_8()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_8())) / 10);
            }
            if (Objects.equals(continueDay,9)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_9()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_9())) / 10);
            }
            if (Objects.equals(continueDay,10)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_10()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_10())) / 10);
            }
            if (Objects.equals(continueDay,11)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_11()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_11())) / 10);
            }
            if (Objects.equals(continueDay,12)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_12()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_12())) / 10);
            }
            if (Objects.equals(continueDay,13)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_13()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_13())) / 10);
            }
            if (Objects.equals(continueDay,14)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_14()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_14())) / 10);
            }
            if (Objects.equals(continueDay,15)){
                return NumUtil.save2Point(Objects.isNull(collect1.get(stationNo).get(0).getRainFall08_15()) ? 999999d : Double.parseDouble(String.valueOf(collect1.get(stationNo).get(0).getRainFall08_15())) / 10);
            }
        }
        return 0d;
    }














}
